What is the difference between Java's BufferedReader
and InputStreamReader
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With