我試圖編寫一個程序來操縱從文件中讀入的unicode字符串。我想到了兩種方法 - 一種是在其中讀取包含換行符的整個文件,執行幾次regex替換,然後將其寫回另一個文件;另一個是我逐行讀取文件並匹配個別行並替換它們並將其寫出。我還沒有能夠測試第一種方法,因爲字符串中的換行符不被寫爲文件的換行符。下面是一些示例代碼來說明:字符串中的換行符不寫出來的文件
String output = "Hello\nthere!";
BufferedWriter oFile = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("test.txt"), "UTF-16"));
System.out.println(output);
oFile.write(output);
oFile.close();
print語句輸出
你好
那裏!
但文件內容
Hellothere!
爲什麼不把我的換行符寫入文件?
記事本無法正確顯示僅有\ n的文本文件。 – akarnokd 2009-07-15 18:39:14
看看它是否仍然與\ r \ n – 2009-07-15 18:42:10