Java BufferedReader
Faster File Reading and Writing in Java
When working with text files in Java, performance becomes important—especially when handling large files. Two commonly used classes that improve file I/O performance are
BufferedReader and BufferedWriter.
These classes use a memory buffer to temporarily store data during reading and writing operations. Instead of accessing the disk for every character, the program processes larger chunks of data at once, which significantly improves performance.
- BufferedReader is used to read text efficiently.
- BufferedWriter is used to write text efficiently.
Both classes are typically combined with FileReader and FileWriter, which handle opening or creating the file.
Reading a Text File Line by Line
One of the main advantages of BufferedReader is the readLine() method, which allows you to read an entire line of text at once.
The following example demonstrates how to read a text file line by line using BufferedReader.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedFileReader {
public static void main(String[] args) {
try (BufferedReader reader =
new BufferedReader(new FileReader("notes.txt"))) {
String currentLine;
while ((currentLine = reader.readLine()) != null) {
System.out.println(currentLine);
}
} catch (IOException error) {
System.out.println("Unable to read the file.");
error.printStackTrace();
}
}
}
Sample Output
Java file handling practice
Understanding buffered streams
Reading files efficiently
How This Program Works
- The program opens the file notes.txt using FileReader.
- A BufferedReader wraps the FileReader to improve reading performance.
- The readLine() method reads one complete line at a time.
- The loop continues until the end of the file is reached (null).
- The try-with-resources statement automatically closes the reader.
Writing Text Using BufferedWriter
BufferedWriter is commonly used when writing large amounts of text to a file. It provides the newLine() method, which adds a platform-independent line break.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedFileWriter {
public static void main(String[] args) {
try (BufferedWriter writer =
new BufferedWriter(new FileWriter("report.txt"))) {
writer.write("Monthly Report");
writer.newLine();
writer.write("Data processing completed successfully.");
writer.newLine();
writer.write("All records have been stored.");
System.out.println("Text written using BufferedWriter.");
} catch (IOException error) {
System.out.println("Error writing to the file.");
error.printStackTrace();
}
}
}
Sample Output
Text written using BufferedWriter.
Why Use BufferedReader and BufferedWriter?
Buffered streams improve file processing performance because they:
- Reduce the number of disk access operations
- Store data temporarily in memory
- Provide convenient methods such as readLine() and newLine()
These features make them ideal for applications that process large text files.