且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 PrintWriter 多次写入文本文件

更新时间:2023-01-23 10:42:31

    try 
    {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename", true)));
    out.println("the text");
    out.close();
     } catch (IOException e) {
    }

FileWriter 构造函数的第二个参数将告诉它追加到文件中(而不是清除文件).

The second parameter to the FileWriter constructor will tell it to append to the file (as opposed to clearing the file).

推荐使用 BufferedWriter 用于昂贵的编写器(即 FileWriter),并且使用 PrintWriter 可以让您访问 println 语法,您可能已经习惯了 System.out.

Using a BufferedWriter is recommended for an expensive writer (i.e. a FileWriter), and using a PrintWriter gives you access to println syntax that you're probably used to from System.out.

但是 BufferedWriterPrintWriter 包装器并不是绝对必要的.

But the BufferedWriter and PrintWriter wrappers are not strictly necessary.