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();
}
}