Efficient Text Processing with Java BufferedReader

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will be using the Java BufferedReader class, which is part of the java.io package. BufferedReader is used to read text from a character input stream and buffers the characters for better performance. The class wraps other reader classes and input streams, allowing us to improve the overall efficiency and performance of our programs. We will explore the different constructors, methods and use cases of BufferedReader in order to understand its functionality.

Create a BufferedReader with a FileReader

The BufferedReader constructor takes a Reader object as a parameter. We can create a reader using a FileReader, which reads text files in the default encoding. In this step, we will read a file with the BufferedReader class by wrapping a FileReader.

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try {
            FileReader fileReader = new FileReader(path);
            BufferedReader bufferedReader = new BufferedReader(fileReader); //Creating a BufferedReader by wrapping the FileReader
            // Add code to read data from the file here

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

To run the code, compile the file with javac BufferedReaderDemo.java, and then run it with java BufferedReaderDemo.

Use a BufferedReader with a Stream

We can also create a BufferedReader using an input stream as a source. In this step, we will wrap an InputStreamReader with BufferedReader and read data from System.in. This will read data that we enter using the keyboard.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BufferedReaderDemo {
    public static void main(String[] args) {
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader bufferedReader = new BufferedReader(isr); //Creating a BufferedReader
            System.out.println("Enter Something:");
            String line = bufferedReader.readLine(); //Read input from console
            System.out.println("You Entered: " + line); //Print out the input

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

Closing the Stream

We should always use the close() method of the BufferedReader to release all the system resources associated with the reader. To simplify the code, we can use a try-with-resources block to automatically close the stream.

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try (FileReader fileReader = new FileReader(path);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            // Add code to read data from the file here

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

BufferedReader Vs. Scanner

Both Scanner and BufferedReader can be used to read data from external sources. However, there are some differences between these two classes. In this step, we will compare some key differences.

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try (FileReader fileReader = new FileReader(path);
             BufferedReader bufferedReader = new BufferedReader(fileReader);
             Scanner scanner = new Scanner(fileReader)) {
            long startTime = System.nanoTime();
            // Reading file with BufferedReader
            bufferedReader.readLine();
            while (bufferedReader.readLine() != null) ;
            long endTime = System.nanoTime();
            System.out.println("Time taken by BufferedReader : " + (endTime - startTime) + "ns");

            startTime = System.nanoTime();
            // Reading file with Scanner
            scanner.nextLine();
            while (scanner.hasNextLine()) scanner.nextLine();
            endTime = System.nanoTime();
            System.out.println("Time taken by Scanner : " + (endTime - startTime) + "ns");

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

This code compares the time taken by BufferedReader and Scanner to read data from the same file. The timings will vary, but the output will tell you which one took longer.

Reading Files Line by Line

The BufferedReader class provides several methods to read data. The readLine() method reads one line at a time. This method returns null if we reach the end of the stream. In this step, we will read a file using BufferedReader.readLine().

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try (FileReader fileReader = new FileReader(path);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {

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

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

Reading Single Characters from a File

We can also read a single character at a time by using the read() method of the BufferedReader class. This method returns the char read as an integer. If we reach the end of the stream, then it returns -1.

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try (FileReader fileReader = new FileReader(path);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            int charRead;
            while ((charRead = bufferedReader.read()) != -1) {
                System.out.print((char) charRead);
            }

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

Reading Multiple Characters from a File

The read() method can read multiple characters at a time. We need to pass a char array in which it will store the data. We also need to use an offset that indicates the starting index of the char array. The stored data starts from this index. We also need to mention the maximum length of characters to read.

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try (FileReader fileReader = new FileReader(path);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            char[] charArr = new char[5];
            bufferedReader.read(charArr, 0, 5);
            System.out.print(charArr);

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

Skipping Characters

The BufferedReader class provides a skip() method, which we can use to skip characters. It takes a long type parameter.

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try (FileReader fileReader = new FileReader(path);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            int charRead;
            StringBuilder sb = new StringBuilder();
            while ((charRead = bufferedReader.read()) != -1) {
                if (charRead != '*') {
                    sb.append((char) charRead);
                } else {
                    bufferedReader.skip(1);
                }
            }
            System.out.println(sb);

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

Mark and Reset

The BufferedReader class provides us the mark() method to mark a specific character. We can come back to this marked character anytime in the future by using the reset() method. The mak() method takes an integer as input that denotes the maximum bytes that can be read before the mark becomes invalid.

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

public class BufferedReaderDemo {
    public static void main(String[] args) {
        String path = "path/to/your/file.txt";
        try (FileReader fileReader = new FileReader(path);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {

            char[] charArr = new char[5];
            charArr[0] = (char) bufferedReader.read();
            charArr[1] = (char) bufferedReader.read();

            bufferedReader.mark(10);
            bufferedReader.skip(1);
            charArr[2] = (char) bufferedReader.read();
            charArr[3] = (char) bufferedReader.read();
            charArr[4] = (char) bufferedReader.read();
            System.out.println(charArr);

            bufferedReader.reset();
            char asterisk = (char) bufferedReader.read();
            System.out.print(asterisk);

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

Summary

In this lab, we used the Java BufferedReader class to read text from a character input stream. We learned how to wrap other reader classes and input streams with BufferedReader to improve performance and efficiency. We covered topics such as reading single and multiple characters from a file, skipping characters, marking and resetting positions in a file, and comparing BufferedReader and Scanner classes. We also used try-with-resources blocks to automatically close the stream and avoid memory leaks.

Other Java Tutorials you may like