What is the difference between Java's BufferedReader and InputStreamReader classes?

JavaInputstreamBufferedreaderInputstreamreader

Java Problem Overview


What is the difference between Java's BufferedReader and InputStreamReader classes?

Java Solutions


Solution 1 - Java

BufferedReader is a wrapper for both "InputStreamReader/FileReader", which buffers the information each time a native I/O is called.

You can imagine the efficiency difference with reading a character(or bytes) vis-a-vis reading a large no. of characters in one go(or bytes). With BufferedReader, if you wish to read single character, it will store the contents to fill its buffer (if it is empty) and for further requests, characters will directly be read from buffer, and hence achieves greater efficiency.

InputStreamReader converts byte streams to character streams. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Hope it helps.

Solution 2 - Java

Reading from main memory is faster than reading from disk/STDIN.

BufferedReader uses a technique called buffering that allows us to reduce how often we read from disk/STDIN by copying chunks to main memory.

Consider:

BufferedReader in = new InputStreamReader(System.in);
in.read(); // 
in.read(); //
// ...
in.read(); // could be hitting the disk/STDIN a lot (slow!)

vs:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
in.read(); //
in.read(); //
// ...
in.read(); // hitting main memory a lot (fast!)

From the documentation: > Without buffering, each invocation of read() could cause bytes to be read from [disk/STDIN], converted into characters, and then returned, which can be very inefficient.

The two classes implement the same interface of Reader. So while you could use just InputStreamReader without BufferedReader, it could result in poor performance. We are just using the decorator pattern here so that we end up with a InputStreamReader which now has a buffering capability.

Solution 3 - Java

Solution 4 - Java

BufferedReader reads a couple of characters from the specified stream and stores it in a buffer. This makes input faster.

InputStreamReader reads only one character from specified stream and remaining characters still remain in the stream.

Example:

class NewClass{    
    public static void main(String args[]) throws IOException{

        BufferedReader isr = new BufferedReader(new InputStreamReader(System.in));
 
        Scanner sc = new Scanner(System.in);
 
        System.out.println("B.R. - "+(char)isr.read());
        System.out.println("Scanner - " + sc.nextLine());       
    }
}

When the isr.read() statement is executed, I entered the input ”hello” and the character “h” of “hello” is printed on the screen. If this was InputStreamReader then the remaining characters “ello” would have remained in the System.in stream and the sc.nextLine() would have printed them. But in this case it doesn’t happens because the BufferedReader reads all of the “hello” characters from the System.in stream and stores them in its own personal buffer and thus the System.in stream remains empty when sc.nextLine() is executed.

For the code:

class NewClass{    

    public static void main(String args[]) throws IOException{

        InputStreamReader isr = new InputStreamReader(System.in);

        Scanner sc = new Scanner(System.in);

        System.out.println("I.S.R. - "+(char)isr.read());
        System.out.println("Scanner - " + sc.nextLine());
            
    }
}

In this case InputStreamReader reads only one character for “hello” input and the remaining “ello” still remain in the System.in stream and these characters are printed by sc.nextLine();

Conclusion:

BufferedReader reads a couple of characters(even if we want only one character it will read more than that) from the Input Stream and stores them in a buffer. That’s why it is called BufferedReader. I was unable to figure out how much characters it read in one go. It varied from 3 to 10 when I tested it for this answer.

InputStreamReader reads only one character from input stream and remaining characters still remain in the stream. There is no intermediate buffer in this case.

When one or more Threads or objects want to read characters from System.in then in that case InputStreamReader should be used because it reads only one character and remaining can be used by other objects or threads.

BufferedReader is fast because it maintains a buffer and retrieving data from buffer is always fast as compared to retrieving data from disk/stdin.

Solution 5 - Java

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer.

InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.

Solution 6 - Java

>BufferedReader is a class in Java that reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, lines and arrays. The buffer size may be specified. If not, the default size, which is predefined, may be used.

>In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore good practice to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

> FileReader reader = new FileReader(“MyFile.txt”); BufferedReader bufferedReader = new BufferedReader(reader); >

>will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

Source: https://medium.com/@isaacjumba/why-use-bufferedreader-and-bufferedwriter-classses-in-java-39074ee1a966

Solution 7 - Java

As I understand that instead of InputStreamReader BufferedReader takes less time to convert the data from bytes to character.so we prefer BufferedReader for better performance

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAjay YadavView Question on Stackoverflow
Solution 1 - JavaamodView Answer on Stackoverflow
Solution 2 - JavaJames LawsonView Answer on Stackoverflow
Solution 3 - JavaMichael Aaron SafyanView Answer on Stackoverflow
Solution 4 - JavaChaitanya VaishampayanView Answer on Stackoverflow
Solution 5 - JavaEhsan MashhadiView Answer on Stackoverflow
Solution 6 - JavaSoft_CoderView Answer on Stackoverflow
Solution 7 - Javauser8732605View Answer on Stackoverflow