Java 的 BufferedReader 和 InputStreamReader 类有什么区别?

发布于 2024-12-04 00:42:02 字数 84 浏览 0 评论 0原文

Java 的 BufferedReaderInputStreamReader 类之间有什么区别?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

久而酒知 2024-12-11 00:42:02

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.

芯好空 2024-12-11 00:42:02

从主内存读取比从磁盘/STDIN 读取更快。

BufferedReader 使用一种称为缓冲的技术,该技术允许我们通过将块复制到主内存来减少从磁盘/STDIN 读取的频率。

考虑:

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

与:

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

来自文档

如果没有缓冲,每次调用read()都可能导致从[磁盘/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:

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.

贱人配狗天长地久 2024-12-11 00:42:02

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.

携余温的黄昏 2024-12-11 00:42:02

BufferedReader 从指定的流中读取几个字符并将其存储在缓冲区中。这使得输入速度更快。

InputStreamReader 仅从指定流中读取一个字符,其余字符仍保留在流中。

示例:

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());       
    }
}

当执行 isr.read() 语句时,我输入了“hello”,屏幕上打印了“hello”的字符“h”。如果这是 InputStreamReader,那么剩余的字符“ello”将保留在 System.in 流中,并且 sc.nextLine() 将打印它们。但在这种情况下,这种情况不会发生,因为 BufferedReader 从 System.in 流中读取所有“hello”字符并将它们存储在自己的个人缓冲区中,因此当 sc.nextLine() 时 System.in 流保持为空。被执行。

对于代码:

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());

    }
}

在这种情况下,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:

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.

羁绊已千年 2024-12-11 00:42:02

BufferedReader 是 Java 中的一个类,它从字符输入流中读取文本,缓冲字符以提供字符、行和数组的高效读取。可以指定缓冲区大小。如果没有,则可以使用预定义的默认大小。

一般来说,Reader 发出的每个读取请求都会导致对底层字符或字节流发出相应的读取请求。因此,最好将 BufferedReader 包装在任何 read() 操作成本可能很高的 Reader 周围,例如 FileReaders 和 InputStreamReaders。例如,

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

将缓冲指定文件的输入。如果没有缓冲,每次调用 read() 或 readLine() 都可能导致从文件中读取字节,转换为字符,然后返回,这可能非常低效。

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

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

太阳哥哥 2024-12-11 00:42:02

据我了解,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文