茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1673059 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_564.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2019/4/5 15:00:39
Appending to file in utf8

For a project I need to append to a textfile using UTF-8 encoding. During my research I found two possibilities:
BufferedWriter out = new BufferedWriter(
    new OutputStreamWriter(new FileOutputStream("file.txt), "UTF-8")
)

This will write to my file in UTF-8, but it will overwrite it, rather than append to it if it already exist.
Then I found the code to apend to an existing file with a parameter in the FileWriter, but this will not use UTF-8 explicitely, rather than use the default system character set:
BufferedWriter out = new BufferedWriter(new FileWriter("myfile.txt", true))

I now need the possibility to define BOTH the encoding as well as appending to a file. Just rely on the system encoding or change this is not an option.



You forgot to add the true paramter to the FileOutputStream constructor:
BufferedWriter out = new BufferedWriter(
    new OutputStreamWriter(
        new FileOutputStream("file.txt", true), // true to append
        StandardCharsets.UTF_8                  // Set encoding
    )
);




try {
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("myFile.txt", true), "UTF-8"));
    out.write("Hello World");
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}


原文出處java - Appending to file in utf8 - Stack Overflow
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|