How to Read a File in Java Using BufferedReader

Reading a file in Java can be done using various classes from the java.io package. One of the most efficient ways to read a file is by using the BufferedReader class, which buffers the input to provide efficient reading of characters, arrays, and lines. This blog post will guide you through the process of reading a file using BufferedReader.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Using BufferedReader to Read a File
  4. Complete Example
  5. Conclusion

Introduction

BufferedReader is a class in the java.io package that provides buffering for Reader instances. It can improve the performance of I/O operations by reducing the number of read operations required to fetch data from the underlying input stream. This tutorial will demonstrate how to use BufferedReader to read a file efficiently.

Prerequisites

Before you start, ensure you have the following:

  • A basic understanding of Java programming
  • A Java development environment (JDK and an IDE like IntelliJ IDEA or Eclipse)

Using BufferedReader to Read a File

To read a file using BufferedReader, you will:

  1. Create a FileReader to read the file.
  2. Wrap the FileReader with a BufferedReader.
  3. Read the file data using the BufferedReader.
  4. Close the streams to release system resources.

Example

The following example demonstrates how to read a file using BufferedReader.

Step 1: Creating a Sample File

First, create a simple text file named example.txt with the following content:

This is an example file.
It contains multiple lines of text.
This is the third line.

Step 2: Reading the File Using BufferedReader

Now, let's read the data from the file using BufferedReader.

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

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

        try (FileReader fr = new FileReader(fileName);
             BufferedReader br = new BufferedReader(fr)) {

            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

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

Explanation:

  • The fileName variable contains the name of the file to be read.
  • A FileReader is created to read the file.
  • A BufferedReader is created to wrap the FileReader.
  • The readLine method of BufferedReader is used to read lines of text from the file.
  • The try-with-resources statement ensures that the streams are closed automatically.

Complete Example

Here is the complete example, including all necessary classes and methods.

example.txt

Create a simple text file named example.txt with the following content:

This is an example file.
It contains multiple lines of text.
This is the third line.

ReadFileUsingBufferedReader.java

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

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

        try (FileReader fr = new FileReader(fileName);
             BufferedReader br = new BufferedReader(fr)) {

            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

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

Conclusion

Reading a file in Java using BufferedReader is an efficient way to handle file input operations. By buffering the input, BufferedReader reduces the number of read operations required, which can improve performance, especially for large files. The example provided demonstrates how to read a file using BufferedReader, and you can experiment with the code to gain a deeper understanding of file I/O in Java.

Feel free to modify and experiment with the code examples provided in this tutorial to suit your specific needs. Happy coding!

Comments