English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collections

Java Set Collections

Java Input Output (I/O)

Java Reader/Writer

Java Other Topics

Java BufferedReader class

In this tutorial, we will learn about Java BufferedReader and its methods with the help of examples.

The BufferedReader class in the java.io package can be used with other readers to read data more efficiently (character by character).

It inherits the abstract class Reader.

How BufferedReader works

BufferedReader maintains an internalof8192a character buffer

During the reading operation in BufferedReader, some characters will be read from the disk and stored in the internal buffer. And characters are read separately from the internal buffer.

This reduces the number of communications with the disk. This is why using BufferedReader can read characters faster.

Create a BufferedReader

To create a BufferedReader, we must first import the java.io.BufferedReader package. After importing the package, we can create the reader.

//Create a FileReader
FileReader file = new FileReader(String file);
//Create a BufferedReader
BufferedReader buffer = new BufferedReader(file);

In the above example, we created a BufferedReader named buffer and a FileReader named file.

Here, the default size of the internal buffer of BufferedReader is8192characters. However, we can also specify the size of the internal buffer.

//Create a BufferedReader with a specified internal buffer size
BufferedReader buffer = new BufferedReader(file, int size);

The buffer will help read characters from the file faster.

Methods of BufferedReader

The BufferedReader class provides implementations for different methods in Reader.

read() method

  • read() - Read a single character from the internal buffer of the reader

  • read(char[] array) - Read characters from the reader and store them in the specified array

  • read(char[] array, int start, int length)- Read the number of characters equal to length from the reader and store them in the specified array starting from the start position

For example, assuming we have a file namedinput.txtwhich contains the following content.

This is a line of text inside the file.

Let's try to read a file using BufferedReader.

import java.io.FileReader;
import java.io.BufferedReader;
class Main {
  public static void main(String[] args) {
    //Create a character array
    char[] array = new char[100];
    try {
      //Create FileReader
      FileReader file = new FileReader("input.txt");
      //Create a BufferedReader
      BufferedReader input = new BufferedReader(file);
      //Read characters
      input.read(array);
      System.out.println("Data in the file: ");
      System.out.println(array);
      //Close reader
      input.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

Data in the file:
This is a line of text inside the file.

In the above example, we created a buffered reader named input. The buffered reader is associated withinput.txtFile link.

FileReader file = new FileReader("input.txt");
BufferedReader input = new BufferedReader(file);

Here, we use the read() method to read a character array from the internal buffer of the buffered reader.

skip() method

To discard and skip a specified number of characters, you can use the skip() method. For example

import java.io.FileReader;
import java.io.BufferedReader;
public class Main {
  public static void main(String args[]) {
    //Create a character array
    char[] array = new char[100];
    try {
      //Assuming the input.txt file contains the following text
      //This is a line of text inside the file.
      FileReader file = new FileReader("input.txt");
      //Create a BufferedReader
      BufferedReader input = new BufferedReader(file);
      //Skip5characters
      input.skip(5);
      //Read characters
      input.read(array);
      System.out.println("Skip")5Characters after the data: ");
      System.out.println(array);
      //Close reader
      input.close();
    }
    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

Skip5Characters after the data:
is a line of text inside the file.

In the above example, we used the skip() method to skip5Characters. Therefore, the characters 'T', 'h', 'i', 's', and ' ' are skipped from the original file.

close() Method

To close the buffered reader, we can use the close() method. After calling the close() method, we will not be able to use the reader to read data.

Other Methods of BufferedReader

MethodDescription
ready()Check if the file reader is ready to read
mark()Mark the position of the data that has been read in the marker reader
reset()Reset the marker to return to the position set by the reader