BufferedWriter对象自带newline()方法可以换行,但如果在字符串中部换行,在想要实现换行的地方加入\r\n就可以了。
例:
public static void main(String[] args) {
try {
File file = new File("d:/BufferedWriter.txt");
Writer fw = new FileWriter(file, true);
// 创建一个字符输出流对象
BufferedWriter pw = new BufferedWriter(fw);
// 输出文字到文件中
pw.write("第1行\r\n第2行");
// 刷新流
pw.flush();
// 关闭流
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}