BufferedReader 中的缓冲区大小是多少?

发布于 2024-10-10 18:20:51 字数 1279 浏览 8 评论 0原文

构造函数中缓冲区大小的意义是什么?

BufferedReader(Reader in, int size)

正如我编写的程序:

import java.io.*;
class bufferedReaderEx{
    public static void main(String args[]){
        InputStreamReader isr = null;
        BufferedReader br = null;
            try{
                isr = new InputStreamReader(System.in);
//              System.out.println("Write data: ");
//              int i = isr.read();
//              System.out.println("Data read is: " + i);
                //Thus the InputStreamReader is useful for reading the character from the stream
                System.out.println("Enter the data to be read by the bufferedReader: ");
                //here isr is containing the lnefeed already so this is needed to be flushed.
                br = new BufferedReader(isr, 2);
                String str = br.readLine();
                System.out.println("The data is : :" +  str);
            }catch(IOException e){
                System.out.println("Can't read: " + e.getMessage());
            }
    }
}

输出:

Enter the data to be read by the bufferedReader: Hello world and hello world again
The data is: Hello world and hello world again

那么缓冲区大小意味着什么,因为我打算只读取两个字符。但事实并非如此。

What is the sense of buffer size in the constructor?

BufferedReader(Reader in, int size)

As i have written the program:

import java.io.*;
class bufferedReaderEx{
    public static void main(String args[]){
        InputStreamReader isr = null;
        BufferedReader br = null;
            try{
                isr = new InputStreamReader(System.in);
//              System.out.println("Write data: ");
//              int i = isr.read();
//              System.out.println("Data read is: " + i);
                //Thus the InputStreamReader is useful for reading the character from the stream
                System.out.println("Enter the data to be read by the bufferedReader: ");
                //here isr is containing the lnefeed already so this is needed to be flushed.
                br = new BufferedReader(isr, 2);
                String str = br.readLine();
                System.out.println("The data is : :" +  str);
            }catch(IOException e){
                System.out.println("Can't read: " + e.getMessage());
            }
    }
}

Output:

Enter the data to be read by the bufferedReader: Hello world and hello world again
The data is: Hello world and hello world again

Then what does the buffer size means as i intended that it would be reading only two characters. but it was not that.

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

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

发布评论

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

评论(3

累赘 2024-10-17 18:20:51

BufferedReader 缓冲输入,正如其名称所示。这意味着它会先从输入源读取到缓冲区,然后再将其传递给您。这里的缓冲区大小是指它缓冲的字节数。

从大多数来源读取输入的速度非常慢。仅 2 个字节的缓冲区就会损害性能,因为您的程序很可能大部分时间都在等待输入。当缓冲区大小为 2 时,读取 100 个字节将导致从内存缓冲区读取 2 个字节(非常快),填充缓冲区(非常慢),从缓冲区读取 2 个字节(非常快),填充缓冲区缓冲区(非常慢)等 - 总体非常慢。当缓冲区大小为 100 时,读取 100 个字节将导致从内存缓冲区读取 100 个字节(非常快)——总体来说非常快。这是假设读取时缓冲区包含 100 个字节,在像您这样的情况下,这是一个合理的假设。

除非您知道自己在做什么,否则应该使用相当大的默认缓冲区大小。缓冲区较小的原因之一是当您在内存有限的设备上运行时,因为缓冲区会消耗内存。

BufferedReader buffers the input, just as the name says. This means that it reads from the input source into a buffer before passing it onto you. The buffer size here refers to the number of bytes it buffers.

Reading input from most sources is very slow. A buffer of just 2 bytes is going to hurt performance, as your program is very likely going to be waiting on input most of the time. With a buffer size of 2, a read of 100 bytes will result in reading 2 bytes from the in-memory buffer (very fast), filling the buffer (very slow), reading 2 bytes from the buffer (very fast), filling the buffer (very slow), etc - overall very slow. With a buffer size of 100, a read of 100 bytes will result in reading 100 bytes from the in-memory buffer (very fast) - overall very fast. This is assuming the buffer is contains the 100 bytes when reading though, which in a case like yours is a reasonable assumption to make.

Unless you know what you're doing, you should use the default buffer size which is quite large. One reason for a smaller buffer is when you are running on a limited-memory device, as the buffer consumes memory.

且行且努力 2024-10-17 18:20:51

http://www.docjar.com/html/api/java/io/BufferedReader。 java.html

根据此 java 文档,默认缓冲区大小为 8192 个字符容量。
行大小被视为 80 个字符的容量。

8192 缓冲区大小足以满足较小的文件大小。但这又是可以增长的。如果文件包含超过 8192 个字符,则 bufferedreader 的 fill 方法将在从文件读取内容之前增加缓冲区大小。对于较大的内容文件,最好在通过构造函数创建缓冲读取器时设置自己的最大缓冲区大小,这样就可以避免重新创建内存并将旧数组复制到新创建的数组中。

http://www.docjar.com/html/api/java/io/BufferedReader.java.html

As per this java documentation, default buffer size is 8192 characters capacity.
Line size is considered as 80 chars capacity.

8192 buffer size is sufficient for smaller file sizes. But again this is growable. if file contains more than 8192 characters, then fill method of bufferedreader will increase the buffer size before reading content from file. For bigger content files preferably set your own max size to buffer while creating buffered reader through constructor, so that you can avoid recreating memory and copying the old array into newly created array.

命硬 2024-10-17 18:20:51

当您读取或写入文件时,您必须访问内核,内核实际上获得了对文件的访问权限。所有的文件操作都必须经过内核。这是一项相当昂贵的操作。缓冲导致读取一大块字节;它们保存在 RAM 中的临时位置,并且是从该位置读取的字节。这样,您就不会频繁请求内核进行文件 IO。

如果您使用巨大的缓冲区大小,您将不必要地占用 RAM。如果您使用一个小型的,您将不断地对内核进行文件请求的窃听。最好允许使用默认值。您可以指定缓冲区大小和实验。大多数机器一次会读取一个扇区或整数个扇区。扇区大小取决于您格式化机器的方式。

下面这个实验很有趣。创建一个包含 1,000,000 个零的文件。使用操作系统的计时功能来查看将其复制到另一个文件的速度(您将编写一个带有缓冲和非缓冲 IO 的复制程序)。使用各种缓冲区大小(包括默认值)对其进行计时。

When you read or write in a file, you must access the kernel, which actually gains access to the file. All file operations must go through the kernel. This is a fairly costly operation. Buffering causes a chunk of bytes to be read; these are held in a temporary location in RAM and are bytes are read in from this location. In this way, you are not making frequent requests of the kernel to do file IO.

If you use a huge buffer size, you will hog up RAM needlessly. If you use a tiny one, you will be bugging the kernel constantly for file requests. It is best to allow the default to be used. You can specify buffer size and experiment. Most machines will read a sector at a time or an integer number of sectors. The sector size depends upon how you format your machine.

The following experiment is interesting. Make a file with 1,000,000 zeroes in it. Use your OS's timing feature to see how fast it copies it to another file (you will write a copy program with buffered and unbuffered IO). Time it with various buffer sizes including the default.

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