开发者

Does DataOutputStream flush automatically when its buffer is full?

开发者 https://www.devze.com 2023-04-11 22:57 出处:网络
I\'m writing information to a file, through a DataOutputStream (RandomAccessFile->FileOutputStream->BufferedOutputStream->DataOutputStream).

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 ou开发者_JAVA技巧t correctly.


DataOutputStream doesn't have a buffer, so there is nothing to flush. Everything is written within the write()/writeXXX() methods. However the BufferedOutputStream 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 the DataOutputStream, not any of the nested streams.

when the data gets too big (4MB atm) my file isn't coming out correctly.

You'll have to post your code. BufferedOutputStream's buffer is 8k bytes by default, nothing to do with 4Mb.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号