使用 FileWriter 逐行写入文本
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFile {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("example.txt"); BufferedWriter buffer = new BufferedWriter(writer)) {
// 写入文件
buffer.write("第一行文本");
// 换行
buffer.newLine();
System.out.println("文件写入成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文来自博客园,作者:小朋友有很多问号,转载请注明原文链接:https://www.cnblogs.com/xuhu/p/17341389.html