Mastering Java BufferedReader: Efficient Character Stream Handling
Java BufferedReader Class
Discover how the BufferedReader class boosts I/O performance in Java with practical examples and in‑depth method explanations.
The BufferedReader class, part of the java.io package, wraps another Reader to provide efficient character input. By reading chunks of data into an internal buffer, it minimizes expensive disk I/O operations.
How BufferedReader Works
Internally, BufferedReader maintains a buffer of 8,192 characters (default size). During a read operation, a block of characters is fetched from the source file and stored in this buffer. Subsequent reads then pull characters from the buffer one by one, drastically reducing the number of disk accesses and speeding up I/O.
Creating a BufferedReader
To use BufferedReader, import it from java.io and wrap any other Reader implementation, such as FileReader:
import java.io.FileReader;
import java.io.BufferedReader;
FileReader file = new FileReader("example.txt");
BufferedReader reader = new BufferedReader(file);
You can also specify a custom buffer size:
BufferedReader reader = new BufferedReader(file, 16384); // 16KB buffer
Key Methods of BufferedReader
read()
int read()– reads a single character from the buffer.int read(char[] cbuf)– reads characters into the supplied array.int read(char[] cbuf, int off, int len)– reads up to len characters into the array starting at offset off.
Example: reading the entire content of input.txt into a character array:
import java.io.FileReader;
import java.io.BufferedReader;
class Main {
public static void main(String[] args) {
char[] buffer = new char[100];
try (FileReader file = new FileReader("input.txt");
BufferedReader reader = new BufferedReader(file)) {
reader.read(buffer);
System.out.println("Data in the file:\n" + new String(buffer));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Data in the file: This is a line of text inside the file.
skip()
The skip(long n) method discards the next n characters from the stream:
reader.skip(5); // skips "This "
After skipping, reading the remaining content yields:
Data after skipping 5 characters: is a line of text inside the file.
close()
Always close the reader to free system resources:
reader.close();
Other Useful Methods
| Method | Description |
|---|---|
ready() | Checks if the stream is ready to be read without blocking. |
mark(int readAheadLimit) | Marks the current position in the stream. |
reset() | Resets the stream to the most recent mark. |
For a complete reference, visit the official Java documentation.
Java
- Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
- Mastering Java FileInputStream: A Practical Guide with Code Examples
- Mastering Java’s ObjectInputStream: A Comprehensive Guide
- Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
- Mastering Java BufferedInputStream: Efficient Byte Reading & Advanced Methods
- Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
- Mastering Java FileReader: Comprehensive Guide & Practical Examples
- Mastering Java FileWriter: A Complete Tutorial
- Java BufferedWriter: Efficient Character I/O Explained
- Mastering Java File Operations with java.io – Creation, Reading, Writing & Deletion