What is the BufferedReader class? A class for reading text files. There is a readline method that reads a text file line by line. There is also a FileReader class, but it seems to be inefficient because it reads characters one by one.
public class Main {
public static void main(String[] args) {
//Check if the file exists by specifying it.
File file = new File("c:\\Sample\\Test.txt");
if(!file.exists()){
System.out.print("File does not exist");
return;
}
//Instead of reading the specified file, the file to be read is specified.
FileReader fileReader = new FileRreader(file);
//The specified file is being read.
BufferedReader bufferedReader = new BufferedReader(fileReader);
String data;
//Read line by line with the readLine method until a null value is output.
while((data = bufferReader.readLine()) != null){
System.out.println(data)
}
fileReader.close();
//Error output.
} catch(IOException e) {
e.printStaticTrace();
}
}
A memo of each class that came out. File class // Specify the file to operate FileReader class // Specify the file to read BufferedReader class // Reading a file.
Recommended Posts