我有一个字符串,其中包含例如“姓名:Foo\n 日期:12/13/11\n”
当我对文本文件执行 BufferedWriter.write(String) 时,它实际上会输出\n 无论如何要对\n 进行替换,以便在写入文本文件时表示换行符?
这是示例代码。
String input = "Name: adfasd \n AN: asdfasdf \n";
Writer textOutput = null;
File file = new File("write.txt");
textOutput = new BufferedWriter(new FileWriter(file));
textOutput.write( input );
textOutput.close();
输出将是
最佳答案
Windows 使用 CR+LF 行结尾而不是 LF 结尾。您应该能够使用 \r\n
获得您想要的结果。
维基百科有一篇关于 newlines 的文章如果你想了解更多。
关于java - 在 BufferedWriter.write() 中替换\n?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8494593/