如何在Java中将新行字符写入文件
我有一个包含新行的字符串。我将此字符串发送到函数以将字符串写入文本文件,如下所示:
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