BufferedReader in Java

In Java, we can use a buffered input class for reading input from various sources such as files, network connections, and input streams.

BufferedReader Class

The BufferedReader Class available in the java.io package, is a crucial component for input stream reading in Java. The main advantage of BufferedReader is its ability to buffer input, which means it can read a chunk of data from the input source and store it in memory for faster access.

BufferedReader Constructor

BufferedReader br = new BufferedReader(Reader r);

We can use this constructor to construct a BufferedReader object with a specified Reader, making it suitable for character-based input operation.

        BufferedReader br = new BufferedReader(Reader r, int bufferSize);

This constructor allows us to define a custom buffer size.

BufferedReader Method

Some important methods of BufferedReader class-

int read()

public int read()
 throws java.io.IOException

This method reads a single character from the input source and returns it’s ASCII value.

void close()

public void close()
 throws java.io.IOException

This method closes the BufferedReader and releases any associated resource.

long skip(long n )

public long skip(long n ) throws java.io.IOException

This method skips over and discards n character from the input source.

String readLine()

public String readLine()
 throws java.io.IOException

This method reads a line of text from an input source, removes a new line character, and returns the line as a String. Return null at the end of Stream.

BufferedReader Example

import java.io.*;

public class BufferedReaderExample {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("java.txt");
        BufferedReader br = new BufferedReader(fileReader);
         String line;
         while ((line = br.readLine())!=null){

Note- When we close BufferedReader automatically underline FileReader will be closed.

The most enhanced reader in Java to read character data from the file is BufferedReader.

Similar Java Tutorials

1 thought on “BufferedReader in Java”

Leave a Comment