简体   繁体   English

DataOutputStream()VS DataOutputStream(new BufferedOutputStream())

[英]DataOutputStream() VS DataOutputStream(new BufferedOutputStream())

The code at Java Tutorials showed an example of using DataOutputStream class and DataInputStream class. Java Tutorials中的代码显示了使用DataOutputStream类和DataInputStream类的示例。

A snippet of the code looks like this: 代码片段如下所示:

//..
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
//..
in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
//..

I was wondering why is it required to create a new BufferedOutputStream when we create a new DataOutputStream ? 我想知道为什么在创建新的DataOutputStream时需要创建一个新的BufferedOutputStream

Isn't it redundant since this alternative works as well? 这不是多余的,因为这个替代方案也有效吗? : new DataOutputStream(new FileOutputStream(dataFile)); new DataOutputStream(new FileOutputStream(dataFile));

As this page claims, DataStreams already provides a buffered file output byte stream. 如此页面声称的那样,DataStreams已经提供了缓冲文件输出字节流。 So is "double-buffering" really required? 那么“双缓冲”真的需要吗?

I've modified the 2 lines of code (output and input), taking away the BufferedOutputStream and BufferedInputStream and everything seems to work just fine, so I was wondering what is the purpose of the BufferedOutputStream and BufferedInputStream ? 我修改了2行代码(输出和输入),取消了BufferedOutputStreamBufferedInputStream ,一切似乎都运行得很好,所以我想知道BufferedOutputStreamBufferedInputStream的目的是什么?

Wrapping the FileOutputStream in a BufferedOutputStream will generally speed up the overall output of your program. 在BufferedOutputStream中包装FileOutputStream通常会加快程序的整体输出速度。 This will only be noticeable if you are writing large amounts of data. 只有在编写大量数据时才会注意到这一点。 The same thing goes for wrapping an InputStream in a BufferedInputStream. 在BufferedInputStream中包装InputStream也是一样的。 The use of buffers will only affect efficiency, not correctness. 缓冲区的使用只会影响效率,而不会影响正确性。

It's not redundant, it's just different. 这不是多余的,只是不同。 The Buffered variants add a buffering layer, speeding up IO operations by batching up reads and writes. Buffered变体添加了一个缓冲层,通过批量读取和写入来加速IO操作。

Instead of going to disk for every read/write, it goes to memory first. 它不是每次读/写都进入磁盘,而是首先进入内存。 How much of a difference it makes depends on a variety of factors. 它产生多大的差异取决于各种因素。 The OS and/or disk I/O system also likely does some buffering. OS和/或磁盘I / O系统也可能进行一些缓冲。

I used to think that the Java IO model was unnecessarily large, but now that I really "get it" I find it quite elegant. 我曾经认为Java IO模型不必要地大,但现在我真的“得到它”我发现它非常优雅。 A BufferedOutputStream is an implementation of the Decorator pattern (google it... it's useful). BufferedOutputStream是Decorator模式的一个实现(google it ...它很有用)。 What this means is that BufferedOutputStream simply adds functionality to the outputstream it wraps. 这意味着BufferedOutputStream只是将功能添加到它包装的输出流中。 Internally, the BufferedOutputStream calls what ever OutputStream it decorates. 在内部,BufferedOutputStream调用它装饰的OutputStream。

Buffered IO streams help you to read in bulk thereby reducing the IO cost significantly. 缓冲IO流可帮助您批量读取,从而显着降低IO成本。 IO perations are fairly costly. IO过程相当昂贵。 Imagine your application doing a full read/write cycle for every byte that is read/written as opposed to reading/writing a chunk of data in one go. 想象一下,您的应用程序对读取/写入的每个字节执行完整的读/写循环,而不是一次读取/写入大量数据。 Doing a Buffered read/write is definitely very efficient. 执行缓冲读/写肯定非常有效。 You will notice a huge difference in efficiency if you gather some performance statistics in both the cases ie w and w/o Buffered IO specially when reading/writing a huge amount of data. 如果你在两种情况下收集一些性能统计数据,你会注意到效率的巨大差异,即w和w / o缓冲IO特别是在读/写大量数据时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM