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屋!
查看全文