Introduction
One of the most widely used classes for reading text efficiently is BufferedReader. The BufferedReader class belongs to the java.io package and is designed to read text from a character input stream using an internal buffer. Instead of reading one character at a time, it reads a larger block of characters into memory, reducing the number of I/O operations and improving performance.
BufferedReader is commonly used with classes like FileReader, InputStreamReader, and StringReader. It also provides the convenient readLine() method, making it easy to read text files line by line.
What is BufferedReader?
BufferedReader is a Java class that reads text efficiently by using an internal character buffer.
Package:
import java.io.BufferedReader;
It is commonly used with:
- FileReader
- InputStreamReader
- StringReader
Unlike FileReader, which reads characters one at a time, BufferedReader reads larger chunks of data into memory, making file reading faster and more efficient.
Why Use BufferedReader?
BufferedReader offers several advantages:
- Faster than FileReader
- Reads one line at a time
- Reduces disk access
- Efficient for large files
- Easy-to-use API
- Suitable for processing text data
It is one of the most commonly used classes for reading text files in Java applications.
How BufferedReader Works?
When you read data using BufferedReader, Java loads a block of characters into an internal buffer.
Instead of accessing the file every time a character is needed, BufferedReader retrieves characters from the buffer. Once the buffer becomes empty, another block of data is loaded automatically.
This buffering mechanism significantly improves performance, especially when reading large files.
Creating a BufferedReader
The most common way is by wrapping a FileReader.
Syntax
BufferedReader reader =
new BufferedReader(new FileReader("students.txt"));
Here:
- FileReader reads characters from the file.
- BufferedReader adds buffering for better performance.
Reading a File Line by Line
The readLine() method reads one complete line at a time.
Example
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
BufferedReader reader =
new BufferedReader(new FileReader("students.txt"));
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Sample File (students.txt)
Rahul
Amit
Priya
Neha
Output
Amit
Priya
Neha
The loop continues until readLine() returns null, indicating the end of the file.
Reading Characters
BufferedReader can also read one character at a time using the read() method.
Example
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
BufferedReader reader =
new BufferedReader(new FileReader("students.txt"));
int character;
while((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Output
Amit
Priya
Neha
Although possible, readLine() is generally preferred for text files.
Reading Keyboard Input
BufferedReader can also read input from the keyboard using InputStreamReader.
Example
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = reader.readLine();
System.out.println("Welcome " + name);
}
}
Output
Welcome Rahul
This approach was commonly used before the Scanner class became popular.
Common BufferedReader Methods
read()
Reads a single character.
int data = reader.read();
Returns:
- Character value
- -1 if the end of the stream is reached
readLine()
Reads an entire line.
String line = reader.readLine();
Returns:
- The next line of text
- null at the end of the file
ready()
Checks whether data is available for reading.
if(reader.ready()) {
System.out.println("Data available");
}
close()
Closes the stream and releases system resources.
reader.close();
Always close the reader after use.
Using try-with-resources (Recommended)
Instead of closing the reader manually, use try-with-resources.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (BufferedReader reader =
new BufferedReader(new FileReader("students.txt"))) {
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
This automatically closes the stream even if an exception occurs.
BufferedReader vs FileReader
| BufferedReader | FileReader |
|---|---|
| Uses an internal buffer | Reads directly from the file |
| Faster | Slower |
| Supports readLine() | Does not support readLine() |
| Suitable for large files | Suitable for simple reading |
| Efficient for text processing | Reads character by character |
Real-Life Example
Suppose an application stores configuration settings in a file named config.txt.
server=localhost
port=8080
mode=production
The application can load these settings using BufferedReader.
BufferedReader reader =
new BufferedReader(new FileReader("config.txt"));
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Many enterprise applications use this approach to load configuration files during startup.
Advantages of BufferedReader
- High performance
- Efficient buffering
- Reads complete lines
- Suitable for large files
- Easy integration with FileReader
- Reduces I/O operations
- Simple API
Limitations of BufferedReader
- Works only with character streams
- Not suitable for binary files
- Requires exception handling
- Does not parse primitive data directly like Scanner
Common Mistakes
1. Forgetting to Close the Reader
Incorrect:
BufferedReader reader =
new BufferedReader(new FileReader("students.txt"));
Correct:
reader.close();
Or use try-with-resources.
2. Ignoring Exceptions
Incorrect:
BufferedReader reader =
new BufferedReader(new FileReader("students.txt"));
Correct:
try {
} catch(IOException e) {
}
3. Using BufferedReader for Binary Files
Incorrect:
BufferedReader reader =
new BufferedReader(new FileReader("image.png"));
Binary files should be read using FileInputStream or BufferedInputStream.
4. Assuming readLine() Includes the Newline Character
readLine() removes the line separator from the returned string.
If you need to preserve line breaks while processing or rebuilding the content, add them manually.
Example:
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append(System.lineSeparator());
}
Best Practices
- Use BufferedReader for reading large text files.
- Prefer readLine() when processing line-based data.
- Always use try-with-resources to ensure streams are closed.
- Handle IOException properly.
- Use BufferedReader with FileReader for text files.
- Use InputStreamReader when reading character input from System.in.
- Use byte streams (FileInputStream, BufferedInputStream) for binary files.
Conclusion
The Java BufferedReader class is one of the most efficient and widely used tools for reading text data. By using an internal buffer, it minimizes disk access, improves performance, and provides the convenient readLine() method for processing text files line by line. It works seamlessly with classes such as FileReader and InputStreamReader, making it suitable for reading files, configuration data, and user input.
For modern Java applications, BufferedReader remains an excellent choice when working with text files, especially large ones. By following best practices—such as using try-with-resources, handling exceptions correctly, and selecting the appropriate stream for text or binary data—you can write efficient, reliable, and maintainable Java applications.