简体   繁体   English

写在文本文件上的文本或字符串与前一个单词重叠。 它应该逐行写入每个字

[英]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.

相关问题 按每个单词查找文本文件的行号 - Find the line number of a text file by each word Java:如何从文本文件的每一行中提取第一个单词并将其写入新的文本文件? - Java: How to take the first word in each line from a text file and write it to a new text file? 将文本文件每行的第一个单词读入ArrayList - Read the first word of each line of a text file into an ArrayList 从文本文件中的一行删除单词 - Delete word from a line in a text file 获取单词在文本文件中的位置(行号,行中的位置) - Get position of a word in a text file (line number, position in line) 如何找到一个单词在文本文件的哪一行中,以及该单词是否存在于多行中,保存行号? - How to find a word is in which line of a text file and if the word exists in multiple line save the line number? (重写文本文件并保留以前的数据)获取值并每次在记事本文件的新行中写入记事本/ ms-word- - (re-writing the text file and keep previous data)get value and write in notepad/ms-word- every time in new line of notepad file 将文本文件的整个行逐行存储在数组中(不是逐字;包含空格) - Store entire line of text file in array line by line (not word by word; include spaces) Android edittext将每行上的文本放入自动换行的数组中? - Android edittext get text on each line into an array with word wrap on? 给定文本文件中单词的偏移量,java程序应检索相应的行号 - Given the offset of a word in a text file, the java program should retrieve respective line number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM