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