只追求优雅,以前从来没这么用过,都是用
BufferedWriter 和OutputStreamWriter来实现
这种方法的好处是可以指定字符编码
但一般情况下,我们是不需要指定编码的,没想到还有PrintWriter这么优雅的办法
BufferedWriter 和OutputStreamWriter来实现
这种方法的好处是可以指定字符编码
但一般情况下,我们是不需要指定编码的,没想到还有PrintWriter这么优雅的办法
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class TestPrinterWriter {
public static void main(String[] args) throws Exception {
String path = TestFile.class.getResource("/").getPath() + "test.txt";
System.out.println(path);
// PrintWriter writer = new PrintWriter(new FileWriter(path, true),true);
PrintWriter writer = new PrintWriter(new FileOutputStream(path), true);
for (int i = 0; i < 10; i++) {
writer.println("文字" + i);
}
writer.flush();
writer.close();
}
}