[英]Text or String written on text file overlaps the previous word. It should write each word line by line
我能够使用此代码访问我创建的文件,该文件将从Android中的EditTExt输入的每个单词保存到文件中。 但是,在输入更多单词后,只有最后一个单词写在文件上。 按下SAVE按钮后似乎覆盖了之前的单词。 每次在文本文件中保存单词时,都会单击此按钮。 我想在文本文件中逐行列出输入的单词。 应该怎么做才能做到这一点?
这是我使用的代码。
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.