BufferedReader
In Java, BufferedReader is a class that provides efficient reading of characters from a character-input stream, which can be a file, console or network socket. It reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
BufferedReader can be used to read text from various sources, such as FileReader, InputStreamReader, StringReader, etc. It provides many methods to read characters and lines from an input stream, including read(), readLine(), and skip(), among others.
Here’s an example of how to use BufferedReader to read a file line by line:
import java.io.*;
public class ReadFile {
public static void main(String[] args) {
try {
File file = new File(“file.txt”);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, we create a File object representing the file we want to read, and pass it to a FileReader object. We then pass the FileReader object to a BufferedReader object, which provides us with the ability to read the file line by line using the readLine() method. It’s important to close the file after we’re done reading it to release the resources used by the FileReader and BufferedReader objects.
Apply for Core Java Developer Certification Now!!
https://www.vskills.in/certification/certified-core-java-developer