[JAVA] [Personal memo] Reading a file using BufferedReader

FileReader is a method that reads character by character. Let's improve the processing efficiency by using BufferingFilter!


package practice1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Read {

	public static void main(String[] args) {

		//FileReader=Movement to read character by character
		//BufferedReader=Read a certain number, accumulate, and release when accumulated (usually line by line)
		
		FileReader fr=null;
		BufferedReader br=null;
		
		//Declared outside the try method so that the close process can be performed in the finally method.
		//If declared in the try method and close in the finally method()Attempting to duplicate local variables.

		try {
			fr=new FileReader("c:\\work\\filetest.txt");
			//C in advance:Create a filetest file in the work folder created in and use it in the following process
			br=new BufferedReader(fr);
			//Combine the functions of reading and storing and releasing!

			String brLine=br.readLine(); //By assigning to brLine, it can be output correctly after reading one sentence.
			while(brLine!=null) {
				System.out.println(brLine);
				brLine=br.readLine();
			}


		}catch(IOException e) {
			e.printStackTrace(); // //A class that displays error details in red on the console
			//System.out.println("A read / write error has occurred");

		}finally { //try-The finally method that is always executed at the end no matter how the catch block works
			if(br!=null) {
				try {
					br.close();
				}catch(IOException e) {}//The contents can be empty, or an error message can be output.
				
			}
			if(fr!=null) {
				try {
					fr.close();
				}catch(IOException e) {}//The contents can be empty, or an error message can be output.
			}
		}
	}
}


Recommended Posts

[Personal memo] Reading a file using BufferedReader
[Personal memo] Writing a file using BufferedWriter
Reading a file using Java's Scanner class
Studying java8 (such as reading a file using Stream)
Upload a file using Java HttpURLConnection
[Personal memo] I learned a little about modifiers
Building a CICD pipeline using Docker (personal memorandum)
Write to a file using ShiftJIS-Read a file (Kotlin / JVM)
A memo when fumbling and developing using JavaFX
[Personal memo] Make a simple deep copy in Java
[Personal application work memo] Make a calendar with simple_calendar
[Personal memo] try-catch summary
Creating a docker host on AWS using Docker Machine (personal memorandum)
Creating a 2021 weekly calendar (refill for personal organizer) using Ruby