Java 的 BufferedReader 和 InputStreamReader 类有什么区别?
Java 的 BufferedReader
和 InputStreamReader
类之间有什么区别?
What is the difference between Java's BufferedReader
and InputStreamReader
classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
BufferedReader 是“InputStreamReader/FileReader”的包装器,每次调用本机 I/O 时都会缓冲信息。
您可以想象读取一个字符(或字节)与读取一个大数字的效率差异。一次性字符(或字节)。使用BufferedReader,如果你想读取单个字符,它会存储内容来填充缓冲区(如果缓冲区为空),对于进一步的请求,将直接从缓冲区读取字符,从而实现更高的效率。
InputStreamReader 将字节流转换为字符流。它读取字节并使用指定的字符集将它们解码为字符。它使用的字符集可以通过名称指定或可以显式给出,或者可以接受平台的默认字符集。
希望有帮助。
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.
从主内存读取比从磁盘/STDIN 读取更快。
BufferedReader 使用一种称为缓冲的技术,该技术允许我们通过将块复制到主内存来减少从磁盘/STDIN 读取的频率。
考虑:
与:
来自文档:
这两个类实现了相同的
Reader
接口。因此,虽然您可以仅使用InputStreamReader
而不使用BufferedReader
,但它可能会导致性能不佳。我们只是在这里使用装饰器模式,以便我们最终得到一个InputStreamReader 其中现在具有缓冲功能。
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:
vs:
From the documentation:
The two classes implement the same interface of
Reader
. So while you could use justInputStreamReader
withoutBufferedReader
, it could result in poor performance. We are just using the decorator pattern here so that we end up with aInputStreamReader
which now has a buffering capability.InputStreamReader 类适应类型 InputStream(未解释的字节)到 Reader 类(字节解释为某些字符集中的字符),但不应用任何额外的缓冲。 BufferedReader 类采用 Reader 类(大概是无缓冲的)并向其应用缓冲。
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 从指定的流中读取几个字符并将其存储在缓冲区中。这使得输入速度更快。
InputStreamReader 仅从指定流中读取一个字符,其余字符仍保留在流中。
示例:
当执行 isr.read() 语句时,我输入了“hello”,屏幕上打印了“hello”的字符“h”。如果这是 InputStreamReader,那么剩余的字符“ello”将保留在 System.in 流中,并且 sc.nextLine() 将打印它们。但在这种情况下,这种情况不会发生,因为 BufferedReader 从 System.in 流中读取所有“hello”字符并将它们存储在自己的个人缓冲区中,因此当 sc.nextLine() 时 System.in 流保持为空。被执行。
对于代码:
在这种情况下,InputStreamReader 只读取“hello”输入的一个字符,剩余的“ello”仍然保留在 System.in 流中,这些字符由 sc.nextLine() 打印;
结论:
BufferedReader 从输入流中读取几个字符(即使我们只想要一个字符,它也会读取更多字符)并将它们存储在缓冲区中。这就是它被称为 BufferedReader 的原因。我无法计算出它一口气读了多少个字符。当我测试这个答案时,它从 3 到 10 不等。
InputStreamReader 仅从输入流中读取一个字符,其余字符仍保留在流中。在这种情况下没有中间缓冲区。
当一个或多个线程或对象想要从 System.in 读取字符时,在这种情况下应该使用 InputStreamReader,因为它只读取一个字符,其余的字符可供其他对象或线程使用。
BufferedReader 速度很快,因为它维护一个缓冲区,并且与从磁盘/标准输入检索数据相比,从缓冲区检索数据总是很快。
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:
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:
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.
来源: https://medium.com/@isaacjumba/why-use-bufferedreader-and-bufferedwriter-classses-in-java-39074ee1a966
Source: https://medium.com/@isaacjumba/why-use-bufferedreader-and-bufferedwriter-classses-in-java-39074ee1a966
据我了解,BufferedReader 比 InputStreamReader 花费更少的时间将数据从字节转换为字符。因此我们更喜欢 BufferedReader 以获得更好的性能
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