2015-08-21 95 views
0

我目前遇到在内部存储器上保存文本文件的问题。如何在内部存储器上持久保存文件

问题是,只要我退出应用程序,该文件似乎被删除。

我写这个方法被调用的应用程序的启动,创建一个空白的文本文件:

private void init() { 
    String FILE_NAME = "save.txt"; 
    try { 
     new BufferedWriter(new FileWriter(getFilesDir() + FILE_NAME)); 
     Toast.makeText(this, "GOOD", Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

调用该函数读取写入其中的所有行:

private List<String> readFromFile() { 
    List<String> ret = new ArrayList<>(); 
    try { 
     InputStream inputStream = new FileInputStream(getFilesDir()+"save.txt"); 
     BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream)); 
     String line; 
     while ((line = bReader.readLine()) != null) { 
      ret.add(line); 
     } 
    } catch (IOException e) { 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
    Toast.makeText(this, "GOOD", Toast.LENGTH_LONG).show(); 
    return ret; 
} 

而且finaly调用此方法追加在文本文件中的字符串,如果它不是已经在它:

private void save(String unNom) { 
    String FILE_NAME = "save.txt"; 
    List<String> ret = new ArrayList<>(); 
    try { 
     InputStream inputStream = new FileInputStream(getFilesDir()+"save.txt"); 
     BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream)); 
     String line; 
     while ((line = bReader.readLine()) != null) { 
      ret.add(line); 
     } 
     if(!ret.contains(unNom)){ 
      ret.add(unNom); 
     } 
     bReader.close(); 
     FileOutputStream fos = new FileOutputStream(getFilesDir() +FILE_NAME); 
     for (String ligne: ret) { 
      ligne+="\n"; 
      fos.write(ligne.toString().getBytes()); 
     } 
     fos.flush(); 
     fos.close(); 
     Toast.makeText(this, "GOOD", Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
    } 
} 

如何正确保存内部存储器中的文件?

对不起,我的英文不好, 谢谢你的帮忙!

回答

0

init不是必需的。我删除了该方法,并编辑了保存方法,因此如果找不到异常,可能会创建一个新文件:

private void save(String unNom) { 
    String FILE_NAME = "save.txt"; 

    List<String> ret = new ArrayList<>(); 

    try { 
     InputStream inputStream = openFileInput("save.txt"); 
     BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream)); 

     String line; 
     while ((line = bReader.readLine()) != null) { 
      ret.add(line); 
     } 
     if (!ret.contains(unNom)) { 
      ret.add(unNom); 
     } 
     bReader.close(); 
     FileOutputStream fos = null; 
     fos = openFileOutput("save.txt", this.MODE_PRIVATE); 
     for (String ligne : ret) { 
      ligne = "\n" + ligne; 
      fos.write(ligne.getBytes()); 
     } 
     fos.close(); 
     Toast.makeText(this, "GOOD", Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     try { 
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
      FileOutputStream fos = null; 
      fos = openFileOutput("save.txt", this.MODE_PRIVATE); 
      fos.write(unNom.getBytes()); 
      fos.close(); 
      Toast.makeText(this, "NEW FILE", Toast.LENGTH_LONG).show(); 
     } 
     catch(Exception e2){} 
    } 
} 
相关问题