BufferedReader Class in Java

In this article, we'll dive deep into the workings and advantages of BufferedReader. 

1. Introduction 

BufferedReader is a part of the java.io package. It reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines. 

2. Why Use BufferedReader? 

Efficiency: Instead of reading one character at a time, BufferedReader reads a larger block (buffer) at once, minimizing I/O operations, which enhances performance. 

Convenience: The readLine() method allows easy reading of a string line. 

3. Constructing a BufferedReader 

The most common practice is to wrap a BufferedReader around a FileReader:

FileReader fr = new FileReader("sample.txt");
BufferedReader br = new BufferedReader(fr);

4. Essential Methods 

Some key functions of BufferedReader include: 

read(): Reads a single character. 

read(char[] cbuf, int off, int len): Reads characters into a portion of an array. 

readLine(): Reads a line of text. 

close(): Closes the reader and releases system resources. 

5. A Simple BufferedReader Example 

Let's glance at a basic example:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderDemo {

    public static void main(String[] args) {
        String fileName = "sample.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

6. Pairing with InputStreamReader 

While it's standard to use BufferedReader with FileReader, it can also wrap around InputStreamReader for reading from other input sources like console input:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();

7. Things to Remember 

Closing Stream: Always ensure you close the BufferedReader after using it. A try-with-resources block, as shown in the example, is a recommended way to handle this. 

Exception Handling: Handle potential IOExceptions when working with BufferedReader.

8. When to Use BufferedReader?

Efficient Text Reading: Utilize BufferedReader when reading large text files or streams. Its internal buffering mechanism greatly improves reading efficiency by reducing the number of I/O operations, as it fetches chunks of characters rather than reading one character at a time. 

Read Lines Conveniently: BufferedReader offers the readLine() method, which simplifies reading files line by line. This is particularly helpful when parsing structured text files like CSVs or reading configuration files. 

Wrapping Other Readers: BufferedReader can wrap around other character-based readers, such as FileReader or InputStreamReader, to augment them with buffering capabilities. This layering approach provides both the data source and the efficient buffered reading capability.

Related Java I/O Classes

Comments