0

我试图通过将每个整数保存在内部存储中文件的新行中来在我的应用程序中保存整数列表。为了检索它,我逐行读取它并将每个行值(解析为整数)放入我的整数列表中。我知道数据库更适合这种东西,但这应该可以。

我现在尝试了很长一段时间,但它似乎从来没有奏效。尝试阅读时,我总是得到一个空指针异常。我记录了“行”,它给出了它应该具有的值。但

保存一个 id,将其添加为新字符串:

private void saveToFavorites(Integer saveFav) {
            String favstr = String.valueOf(saveFav);
            BufferedWriter writer = null;
            try {
              writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("favorites", MODE_WORLD_WRITEABLE)));
              writer.newLine();
              writer.append((favstr));
              System.out.println(" added to favs :"+ saveFav);
            } catch (Exception e) {
                    e.printStackTrace();
            } finally {
              if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
              }
            }
        } 

以及阅读方法:

  @SuppressWarnings("null")
    private List<Integer> readFileFromInternalStorage() {
            List<Integer> favs = null;
            BufferedReader input = null;
            try {
              input = new BufferedReader(new InputStreamReader(openFileInput("favorites")));
              String line;
              while ((line = input.readLine()) != null) {
                  System.out.println("readFileFromInternalStorage line value: "+ line );
                 favs.add(Integer.parseInt(line));
              }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("readFileFromInternalStorage: fail" );
            } finally {
            if (input != null) {
              try {
                input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
              }
            }
            return favs;
        }  

这是在其他活动中。我认为它会工作,但它显然没有。回读时,logline: System.out.println("readFileFromInternalStorage line value: "+ line ); 显示 line 的值等于最后添加的 id 和一个空行,而不是其他行。所以逐行保存失败。同样,当将其解析为整数时,它会失败,这很奇怪,因为它只是一个数字。

08-01 12:29:54.190: I/System.out(1540): readFileFromInternalStorage line value: 
08-01 12:29:54.190: I/System.out(1540): readFileFromInternalStorage line value: 301

有谁知道我需要改变什么?

4

4 回答 4

3

因为我建议序列IntegerSerializable整个List

private void saveList(List<Integer> list) {
try {
            File file = Environment.getExternalStorageDirectory();
            File filename = new File(file, "yourfilename");
            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(list);
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
}

private void readList()
{


    try {
                File file = Environment.getExternalStorageDirectory();
                File filename = new File(file, "yourfilename");
                fis = new FileInputStream(filename);
                in = new ObjectInputStream(fis);
                List<Integer> list= (List<Integer>) in.readObject();
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }

}
于 2012-08-01T10:47:25.497 回答
1

试试这个它可以帮助你:- 1 - 字符串 saveFav = 包含所有整数,这种形式 I1+"/"I2+"/"I3;

2:-然后将其保存到文件中

     private void saveToFavorites(String saveFav) { 
       //right here your code for write into file saveFave string
 }  

在读取文件读取字符串和拆分(“/”)。它为我工作。

于 2012-08-01T11:40:00.570 回答
0

这是一些工作代码,可以读取和写入手机内部存储器的整数。您可以创建一个数组或整数列表,基本上只是对其进行迭代,直到所有整数都保存/读取到内存/从内存中读取:

这是将 int 写入内存的代码:

public void writePrimitiveInternalMemory(String filename, int value) {
        SharedPreferences preferences = this.getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(filename, value);
        editor.commit();

    }

这是从内存中读取的代码:

public int readPrimitiveInternalMemoryInteger(String filename) {
        SharedPreferences preferences = this.getPreferences(Activity.MODE_PRIVATE);
        return preferences.getInt(filename, 0);

    }

我希望这可以帮助你!

于 2012-08-01T10:46:09.667 回答
0

您没有分配整数列表...

List<Integer> favs = null;

分配一个新的arraylist..

List<Integer> favs = new ArrayList<Integer>();
于 2012-08-01T10:50:17.107 回答