簡體   English   中英

寫在文本文件上的文本或字符串與前一個單詞重疊。 它應該逐行寫入每個字

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM