2012-08-01 68 views
0

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

我想了很长一段时间,但它似乎从来没有工作。试图阅读时,我总会得到一个nullpointerexception。我登录“行”,它给了它应该有的价值。但

节省一个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); 显示行的值等于最后添加的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 

任何人都知道我需要改变什么?

回答

3

由于IntegerSerializable我sugget序列化整个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(); 
      } 

} 
+0

我想这个,但我得到一个错误:android filenotfoundexception没有这样的文件或目录,为字符串我把文件名。我应该放什么? write_external_memory在我的清单中,那不是它 – Jasper 2012-08-01 11:14:52

+1

我编辑了我的答案。 – Blackbelt 2012-08-01 11:52:13

+0

工作很完美,谢谢你 – Jasper 2012-08-01 14:43:39

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); 

    } 

我希望这可以帮助你!

+0

由于共享先决条件,我遇到了一些麻烦,因为它没有在我想要的时候提交,在之前询问了有关该行为的一些问题,这就是我选择内部存储的原因。 – Jasper 2012-08-01 14:46:46

0

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

List<Integer> favs = null; 

分配一个新的ArrayList ..

List<Integer> favs = new ArrayList<Integer>(); 
1

试试这个可以帮助哟u: - 1 - 字符串saveFav =包含此形式的所有整数I1 +“/”I2 +“/”I3;

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

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

在阅读文件中读取字符串和分裂( “/”)这是为我工作。