DataOutputStream 缓冲区满时会自动刷新吗?
我正在通过 DataOutputStream (RandomAccessFile->FileOutputStream->BufferedOutputStream->DataOutputStream)将信息写入文件。
我假设如果用于数据输出的缓冲区已满,那么数据输出流会自动刷新?
我问的原因是我正在 for 循环中写入数据,并在循环后刷新(我猜测循环的每次迭代后刷新会破坏使用缓冲区的点),并且当数据变得太小时大 (4MB atm) 我的文件无法正确输出。
I'm writing information to a file, through a DataOutputStream (RandomAccessFile->FileOutputStream->BufferedOutputStream->DataOutputStream).
I assume that if the buffer used for data output is filled, then the dataoutput stream would automatically flush?
The reason I ask is that I'm writing the data in a for loop, and flushing after the loop (I'm guessing that flushing after every iteration of the loop would destroy the point of using buffers), and when the data gets too big (4MB atm) my file isn't coming out correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataOutputStream
没有缓冲区,因此无需刷新。所有内容都写在write()/writeXXX()
方法中。但是 BufferedOutputStream 当然有一个缓冲区,因此您当然需要刷新或关闭才能将数据写入文件。您需要关闭最外层的流,即本例中的 DataOutputStream,而不是任何嵌套的流。您必须发布您的代码。
BufferedOutputStream
的缓冲区默认是8k字节,与4Mb无关。DataOutputStream
doesn't have a buffer, so there is nothing to flush. Everything is written within thewrite()/writeXXX()
methods. However theBufferedOutputStream
has a buffer of course, so you certainly need to flush or close to get that data written to the file. You need to close the outermost stream, i.e. in this case theDataOutputStream
, not any of the nested streams.You'll have to post your code.
BufferedOutputStream
's buffer is 8k bytes by default, nothing to do with 4Mb.