JavaBufferedReader
BufferedReader and BufferedWriter
BufferedReader andBufferedWriter make reading and writingtext files faster.
BufferedReaderlets you read textline by line withreadLine().BufferedWriterlets you write text efficiently and add new lines withnewLine().
These classes are usually combined withFileReader andFileWriter, which handle opening or creating the file. The buffered classes then make reading/writingfaster by using a memory buffer.
Read a Text File (Line by Line)
UseBufferedReader withFileReader to read each line of a file:
Example
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class Main { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("filename.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println("Error reading file."); } }}The output will be:
Some text from filename.txtComparing File Reading Classes
Java gives you several ways to read files. Here's when to pick each one:
Scanner- best forsimple text. It can split text into lines, words, or numbers (e.g.,nextInt(),nextLine()).BufferedReader- best forlarge text files. It is faster, uses less memory, and can read full lines withreadLine().FileInputStream- best forbinary files (like images, PDFs, or audio)

