What is the difference between Java's BufferedReader and InputStreamReader classes?
What is the difference between Java's BufferedReader
and InputStreamReader
cl开发者_StackOverflow社区asses?
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.
The InputStreamReader class adapts type InputStream (uninterpreted bytes) to the Reader class (bytes interpreted as characters in some character set), but does not apply any additional buffering. The BufferedReader class takes a Reader class (presumably unbuffered) and applies buffering to it.
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.
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.
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
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
精彩评论