[英]Text or String written on text file overlaps the previous word. It should write each word line by line
I am able to access the file I created using this code that saves into the file every word inputted from EditTExt in Android. 我能够使用此代码访问我创建的文件,该文件将从Android中的EditTExt输入的每个单词保存到文件中。 However, after inputting more words the only the last word was written on the file. 但是,在输入更多单词后,只有最后一个单词写在文件上。 It seems that the previous words was overwritten after pressing the SAVE button. 按下SAVE按钮后似乎覆盖了之前的单词。 This button is clicked each time a word is to be saved in the text file. 每次在文本文件中保存单词时,都会单击此按钮。 I wanted to list the word inputted line by line on the text file. 我想在文本文件中逐行列出输入的单词。 What should be done to do this? 应该怎么做才能做到这一点?
here's the code i used. 这是我使用的代码。
File dir = new File (getFilesDir(), "myFolder");
dir.mkdirs();
File file = new File(dir, "myData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("\n" + stringWord);
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream
以“覆盖”模式打开文件,以附加到文件使用:
FileOutputStream f = new FileOutputStream(file, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.