ByteArrayOutputStream 和 BufferedOutputStream 的区别 [英] Difference between ByteArrayOutputStream and BufferedOutputStream

查看:35
本文介绍了ByteArrayOutputStream 和 BufferedOutputStream 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ByteArrayOutputStreamBufferedOutputStream 都通过将数据放入内存中的数组来进行缓冲.所以我的问题是

Both ByteArrayOutputStream and BufferedOutputStream do buffering by placing data in an array in memory. So my questions are

  1. 这两者之间有什么区别.
  2. 何时使用 ByteArrayOutputStream 以及何时使用 BufferedOutputStream
  1. what are the differences between these two.
  2. When to use ByteArrayOutputStream and when to use BufferedOutputStream

有人可以帮助我解决上述两个问题,因为我对此感到困惑.

Can some one help me in above two questions as i am confused on this.

推荐答案

看看javadoc:

ByteArrayOutputStream:

这个类实现了一个输出流,其中数据被写入一个字节数组.

This class implements an output stream in which the data is written into a byte array.

BufferedOutputStream:

该类实现了一个缓冲输出流.通过设置这样的输出流,应用程序可以将字节写入底层输出流,而不必为写入的每个字节调用底层系统.

The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

所以,这实际上是两种截然不同的东西:

So, those are really two very different things:

  • 当您知道自己有一些最终需要作为字节数组的数据时使用的第一个
  • 第二个只是围绕任何其他类型的输出流的包装器 - 这增加了缓冲.
  • the first one you use when you know that you have some data that in the end you need as array of bytes
  • the second one is just a wrapper around any other kind of output stream - which adds buffering.

这就是全部!

如果您想体验不同的行为:创建一个写入文件的缓冲行为和一个数组行为.然后继续将字节推入每个字节.阵列一个会在某些时候导致内存问题,另一个可能不会停止,直到您的所有磁盘空间都用完.

And if you want to experience a different behavior: create a buffered one that writes to a file, and an array one. Then just keep pushing bytes into each one. The array one will cause a memory problem at some point, the other one might not stop until all of your disk space is used up.

这篇关于ByteArrayOutputStream 和 BufferedOutputStream 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆