写入文件的字符串不保留换行符
我正在尝试写一个(冗长但包装),这是从.当字符串打印到控制台时,格式与 中的格式相同,但是当我使用 BufferedWriter 将它们写入文件时,它是在一行中写入的。String
JTextArea
Text Area
String
以下代码段可以重现它:
public class BufferedWriterTest {
public static void main(String[] args) throws IOException {
String string = "This is lengthy string that contains many words. So\nI am wrapping it.";
System.out.println(string);
File file = new File("C:/Users/User/Desktop/text.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(string);
bufferedWriter.close();
}
}
哪里出错了?如何解决这个问题?感谢您的任何帮助!