如何在内部存储器上持久保存文件

How to save durably a file on internal storage

我目前在将文本文件保存到内部存储器时遇到问题。

问题是每当我退出应用程序时,文件似乎都被删除了。

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

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

最后调用此方法在文本文件中追加字符串(如果文本文件中不存在的话):

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

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

抱歉我的英语不好, 感谢您的帮助!

不需要初始化。我删除了该方法并编辑了保存方法,因此如果引发未找到异常,它可以创建一个新文件:

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){}
    }
}