本文介绍了追加到文件的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,我需要添加到我的文件,但它不能正常工作,它使覆盖文件,任何人都可以请告诉我什么是错的:
hey there, i need to append to my file but it is not working, it keeps overwriting the file, can anyone please tell me what is wrong:
public void generateNoteOnSD(String sBody){
try
{
File root = new File(Environment.getExternalStorageDirectory(), "AdidasParticipants");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, "participants.txt");
BufferedWriter bW;
bW = new BufferedWriter(new FileWriter(gpxfile));
bW.write(sBody);
bW.newLine();
bW.flush();
bW.close();
//Toast.makeText(mContext, "Tus datos han sido guardados", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
e.printStackTrace();
// importError = e.getMessage();
// iError();
}
}
在此先感谢。
Thanks in advance.
推荐答案
您可以通过更改中可以分配线路修复它的的BufferedWriter
:
You can fix it by changing the line where you assign the BufferedWriter
:
bW = new BufferedWriter(new FileWriter(gpxfile, true));
当您打开使用的FileWriter
的构造函数,只需要在文件中的文件
,它将覆盖什么previously的文件中。直供第二个参数为真
通知的FileWriter
要追加到它的结束。
When you open a file using the FileWriter
constructor that only takes in a File
, it will overwrite what was previously in the file. Supplying the second parameter as true
tells the FileWriter
that you want to append to the end of it.
这篇关于追加到文件的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!