[英]How do I add to the file i created on my android app?
this is my code: 这是我的代码:
FileOutputStream fOut = openFileOutput("things", Context.MODE_PRIVATE);
fOut.write(thing.getBytes());
everytime i go through this method it erases the previous data in the file and overwrites it with the new one. 每次我使用此方法时,它都会擦除文件中的先前数据,并用新的数据覆盖它。 I don't want this to happen because I want the text to be added Thanks for your help.
我不希望发生这种情况,因为我希望添加文本,谢谢您的帮助。
Add the flag MODE_APPEND : 添加标志MODE_APPEND :
FileOutputStream fOut = openFileOutput("things", MODE_APPEND);
fOut.write(thing.getBytes())
other option would be using of course the append()
method: 其他选择当然是使用
append()
方法:
FileOutputStream fOut = new FileOutputStream(myFile,true);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("Something new!\n");
myOutWriter.close();
fOut.close();
您需要在openFileOutput调用中将MODE_APPEND标志(按位或)添加到MODE_PRIVATE中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.