[Personal memo] Writing a file using BufferedWriter

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

[Personal memo] Writing a file using BufferedWriter
[Personal memo] Reading a file using BufferedReader
Upload a file using Java HttpURLConnection
Reading a file using Java's Scanner class
Scraping and writing specific elements to a file
Building a CICD pipeline using Docker (personal memorandum)
A memo when fumbling and developing using JavaFX
Studying java8 (such as reading a file using Stream)
[Personal memo] Make a simple deep copy in Java
[Personal application work memo] Make a calendar with simple_calendar
JavaFX8 file writing (TextArea)
Spring boot memo writing (1)
[Personal memo] try-catch summary
Spring boot memo writing (2)
JavaFX8 file writing (TextField)
Creating a docker host on AWS using Docker Machine (personal memorandum)
Creating a 2021 weekly calendar (refill for personal organizer) using Ruby