Android保存到file.txt附加 [英] Android save to file.txt appending
本文介绍了Android保存到file.txt附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我承认我仍在学习,并且会认为自己是编程方面的新手(最多)。我无法在android中附加文件。每当我保存时,它将覆盖文件,并且我在理解如何保留已存在的文件并仅添加新行方面遇到麻烦。希望有一些澄清/建议。这是我保存到文件的方式(每次保存时都会重写文件)。
I admittedly am still learning and would consider myself a novice (at best) regarding programming. I am having trouble with appending a file in android. Whenever I save, it will rewrite over the file, and I am having trouble understanding how to keep the file that is already there and only add a new line. Hoping for some clarity/advice. Here is how I am saving to the file (which rewrites the file each time I save).
public void saveText(View view){
try {
//open file for writing
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", MODE_PRIVATE));
//write information to file
EditText text = (EditText)findViewById(R.id.editText1);
String text2 = text.getText().toString();
out.write(text2);
out.write('\n');
//close file
out.close();
Toast.makeText(this,"Text Saved",Toast.LENGTH_LONG).show();
} catch (java.io.IOException e) {
//if caught
Toast.makeText(this, "Text Could not be added",Toast.LENGTH_LONG).show();
}
}
推荐答案
更改
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", MODE_PRIVATE));
至,
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("save.txt", Context.MODE_APPEND));
这会将您的新内容附加到已经存在的文件中。
This will append your new contents to the already existing file.
我希望能帮上忙!
这篇关于Android保存到file.txt附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文