我有一个包含新行的字符串。我将此字符串发送到函数以将字符串写入文本文件,如下所示:
public static void writeResult(String writeFileName, String text)
{
try
{
FileWriter fileWriter = new FileWriter(writeFileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(text);
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println("Error writing to file '"+ writeFileName + "'");}
} //end writeResult function
但是当我打开文件时,我发现它没有任何新行。当我在控制台屏幕中显示文本时,它会以新行显示。如何在文本文件中写入换行符。
编辑:
假设这是text
我发送给上述函数的参数:
I returned from the city about three o'clock on that
may afternoon pretty well disgusted with life.
I had been three months in the old country, and was
如何在文本文件中按原样(使用新行)写入此字符串。我的函数将字符串写在一行中。您能否提供一种将文本写入文件的方法,包括换行符?
编辑 2: 文本最初位于 .txt 文件中。我使用以下方法阅读文本:
while((line = bufferedReader.readLine()) != null)
{
sb.append(line); //append the lines to the string
sb.append('\n'); //append new line
} //end while
sb
StringBuffer在哪里