Peter
Peter

Reputation: 1471

Does DataOutputStream flush automatically when its buffer is full?

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.

Upvotes: 1

Views: 4912

Answers (1)

user207421
user207421

Reputation: 311052

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.

Upvotes: 3

Related Questions