Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 522
Ajay Yadav Avatar asked Sep 11 '11 06:09

Ajay Yadav


People also ask

What is the difference between BufferedReader and Scanner classes?

Scanner and BufferReader both classes are used to read input from external system. Scanner is normally used when we know input is of type string or of primitive types and BufferReader is used to read text from character streams while buffering the characters for efficient reading of characters.

What is the difference between BufferedReader and BufferedInputStream?

The main difference between BufferedReader and BufferedInputStream is that BufferedReader reads characters (text), whereas the BufferedInputStream reads raw bytes. The Java BufferedReader class is a subclass of the Java Reader class, so you can use a BufferedReader anywhere a Reader is required.

What is the difference between FileReader and InputStreamReader in Java?

FileInputStream is Byte Based, it can be used to read bytes. FileReader is Character Based, it can be used to read characters. FileInputStream is used for reading binary files. FileReader is used for reading text files in platform default encoding.

What is the difference between Scanner and InputStreamReader?

InputStreamReader, with a large enough buffer, can perform on par with BufferedReader, which I remember to be a few times faster than Scanner for reading from a dictionary list. Here's a comparison between BufferedReader and InputStreamReader. Remember that BufferedReader is a few times faster than Scanner.


2 Answers

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.

like image 92
amod Avatar answered Sep 28 '22 01:09

amod


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.

like image 43
James Lawson Avatar answered Sep 28 '22 02:09

James Lawson