我正在使用以下函数将字符串写入文件。该字符串使用换行符格式化。Java:BufferedWriter跳过换行
例如,text = "sometext\nsomemoretext\nlastword";
我能看到的输出文件的换行符,当我做:
type outputfile.txt
然而,当我在记事本中打开文本我看不到换行。一切都显示在一条线上。
为什么会发生这种情况。我如何确保正确写入文本以便能够在记事本中正确(格式化)地看到文本。
private static void FlushText(String text, File file)
{
Writer writer = null;
try
{
writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (writer != null)
{
writer.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
哦。现在我正在使用System.getProperty(“line.separator”);无法使用BufferedWriter.newLine(),因为在写入之前需要填充完整的字符串。 – devnull 2010-11-01 06:28:42
这救了我。我正在使用Wicket,BufferedWriter.newLine()在本地工作,但不在tomcat上。我尝试了\ r \ n,它的工作方式就像一个魅力。谢谢! – eaglei22 2014-01-08 22:25:33