Verbatim https://www.dazhuanlan.com/2019/08/25/5d622ab9a21fa/
This article summarizes the actions related to the use of the class to write a document
1. FileOutputStream
1 |
public static void writeFile1() throws IOException { |
This example used a FileOutputStream, you can use PrintWriter FileWriter or instead of the operation process txt document format
2. FileWriter
1 |
public static void () throws IOException { |
3. PrintWriter
1 |
public static void writeFile3() throws IOException { |
4. OutputStreamWriter
1 |
public static void writeFile4() throws IOException { |
The difference between them
From Java Doc
The main difference is that, PrintWriter provide some additional methods and formats such as println printf. In addition, FileWriter throws an exception to prevent any kind of I / O failure.
PrintWriter does not throw IOException, which may be provided using a type checkerror boolean flag bit obtained in (). PrintWriter automatically calls flush after each data byte is written. It involves FileWriter, the caller needs to pay attention to the use of flush.