BufferedReader Class in Java

Introduction

The BufferedReader class is a subclass of Reader and is designed to read text from an input stream by buffering characters. Buffering can improve the performance of I/O operations by reducing the number of physical reads from the underlying source. The BufferedReader reads chunks of data into a buffer, from which the data is then read as needed.

Table of Contents

  1. Creating a BufferedReader
  2. Reading Data from a BufferedReader
  3. Closing the Stream
  4. Complete Example
  5. Conclusion

Creating a BufferedReader

To create a BufferedReader, you need to wrap it around another Reader, such as a FileReader. You can also specify the size of the buffer, although a default size is provided if you do not specify one.

Example

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

public class BufferedReaderExample {
    public static void main(String[] args) {
        try {
            // Creating BufferedReader using FileReader
            FileReader fr = new FileReader("example.txt");
            BufferedReader br = new BufferedReader(fr);

            // Creating BufferedReader with specified buffer size
            BufferedReader brWithBufferSize = new BufferedReader(fr, 8192);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

In the example above, BufferedReader is created using a FileReader. Additionally, a BufferedReader with a specified buffer size of 8192 characters is also created.

Reading Data from a BufferedReader

The BufferedReader class provides several methods to read data from the input stream:

  • int read(): Reads a single character.
  • int read(char[] cbuf, int off, int len): Reads up to len characters into an array starting at offset off.
  • String readLine(): Reads a line of text.

Example

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

public class BufferedReaderReadExample {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the example above, BufferedReader reads data from a file named example.txt. It reads the file line by line and prints each line to the console.

Closing the Stream

It is important to close the BufferedReader after completing the file operations to release the system resources associated with the stream. This can be done using the close() method.

Example

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

public class BufferedReaderCloseExample {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("example.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

In the example above, the BufferedReader is closed in the finally block to ensure that it is closed even if an exception occurs.

Alternatively, you can use the try-with-resources statement, which ensures that the stream is closed automatically.

Example

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

public class BufferedReaderTryWithResourcesExample {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the example above, the try-with-resources statement is used to automatically close the BufferedReader.

Complete Example

Here is a complete example demonstrating the creation, reading, and closing of a BufferedReader.

BufferedReaderExample.java

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

public class BufferedReaderExample {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, a BufferedReader is created, data is read from a file, and the stream is automatically closed using the try-with-resources statement.

Conclusion

The BufferedReader class in Java is used for improving the performance of input operations by buffering the input data. By understanding how to create, read, and close a BufferedReader, you can effectively handle file I/O operations involving large amounts of text data in your Java applications. Remember to always close the stream after use to ensure that system resources are properly released.

Comments