FileWriter is a method that writes characters one by one. Let's improve the processing efficiency by using BufferingFilter!
package practice1;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Write {
public static void main(String[] args) {
FileWriter fw=null;
BufferedWriter bw=null;
try {
//Create a work file in the c drive in advance
fw=new FileWriter("c:\\work\\filetest.txt",true);
//Generate a filetest file in the work folder. Use it if any
bw=new BufferedWriter(fw);
/*To write line by line, create a Buffered method that "stores and releases a fixed amount"
Combine with FileWriter (write method)*/
bw.write("abcdefg"); //writing. Thanks to the Buffered method, you can write line by line.
bw.newLine(); //Line feed processing
bw.write("1234567890");
bw.newLine();
bw.write("1 2 3 4 5 6 7 8 9 0");
bw.flush(); //A method that forcibly commands "Write now!"
}catch(IOException e){
e.printStackTrace(); //Method to display error details in red on the console
//System.out.println("File write error");
/* try-Whether or not an error occurs between catches
*Use the fially method so that the file can be closed ↓*/
}finally {
if(bw!=null) {
try {
bw.close();
}catch(IOException e) {}//The contents can be empty, or even if you output some error message, it's ok
}
if(fw!=null) {
try {
fw.close();
}catch(IOException e) {}
}
}
}
}
Recommended Posts