java BufferedOutputStream 的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5561815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the purpose of BufferedOutputStream?
提问by user496949
I am wondering the purpose of BufferedOutputStream, performance gain when using it?
我想知道 BufferedOutputStream 的目的,使用它时的性能提升?
采纳答案by Dante May Code
Here is the line from API of BufferedOutputStream:
这是BufferedOutputStream 的 API 中的一行:
The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream withoutnecessarily causing a call to the underlying system for each byte written.
该类实现了一个缓冲输出流。通过设置这样的输出流,应用程序可以将字节写入底层输出流,而不必为写入的每个字节调用底层系统。
It can do most of operations within the buffer, and without a call to the underlying system.
它可以在缓冲区内完成大部分操作,而无需调用底层系统。
For example, consider writing to file: without buffer, it has to make a system call for every single byte, which is obviously slow.
例如,考虑写入文件:没有缓冲区,它必须对每个字节进行系统调用,这显然很慢。
回答by StaxMan
As its name suggests, BufferOutputStream
has an internal buffer (byte[]
) to which contents of individual small writes are first copied. They are written to the underlying OutputStream
when buffer is full, or the stream is flushed, or the stream is closed.
This can make a big difference if there is a (relatively large) fixed overhead for each write operation to the underlying OutputStream
, as is the case for FileOutputStream
(which must make an operating system call) and many compressed streams.
顾名思义,BufferOutputStream
有一个内部缓冲区 ( byte[]
),首先将各个小写的内容复制到该缓冲区。OutputStream
当缓冲区已满、流被刷新或流被关闭时,它们被写入底层。如果对底层的每个写操作都有(相对较大的)固定开销,这可能会产生很大的不同OutputStream
,就像FileOutputStream
(必须进行操作系统调用)和许多压缩流的情况一样。
At the same time, many stream-based libraries use their own buffering (like XML and JSON writers), and use of BufferedOutputStream
provides no benefit. But its own overhead is relatively low so there isn't much risk.
同时,许多基于流的库使用它们自己的缓冲(如 XML 和 JSON 编写器),并且使用BufferedOutputStream
没有任何好处。但其自身的开销相对较低,因此风险不大。
回答by evilone
BufferedOutputStream provides output data buffering which increases efficiency by storing values to be written in a buffer and actually writing them out when the buffer fills or when the flush() method is called.
BufferedOutputStream 提供输出数据缓冲,它通过将要写入的值存储在缓冲区中并在缓冲区填满或调用 flush() 方法时实际将它们写出来提高效率。
回答by Ali Murtaza K-17SW40
Java BufferedOutputStream class is used for buffering an output stream. It internally uses buffer to store data. It adds more efficiency than to write data directly into a stream. So, it makes the performance fast.
Java BufferedOutputStream 类用于缓冲输出流。它内部使用缓冲区来存储数据。与将数据直接写入流相比,它提高了效率。因此,它使性能快速。