Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BufferedOutPutStream.flush() not writing all bytes to disk?

807580Mar 24 2010 — edited Mar 24 2010
Debuging some code where we use a BufferedOutputStream wrapped around a FileOutputStream to write data out to a disk.
We have some code that try's to check the current file size on the disk by first calling flush() on the stream, and then trying to open the File and use the length() method to get the current file size on the disk. We hare trying to get the file size on the disk without closing the stream. I have always thought that flush() forces all data to be written out.

I have noticed one small issue which happens when we have a small amount of data in the stream ( Less than about 8K bytes). Calling the flush() method on the stream seems to not write any data out to the file as the File.length() method returns zero bytes. It is not until we call the close() method on the file is called will it return the correct file size of about 6K bytes.

I was out looking at the API's and coudn't find anything where the flush() method would only write out data if it is larger than a certain chunk size. Just wondering if anyone else has come across this type of issue?

Any input would be helpful. Thanks in advance!
// Our Buffered output stream to the file
BufferedOutputStream out = new BufferedOutputStream(  new FileOutputStream( this.fullPathFileName ) );

//...

// Try to get a current disk file size without closing the stream.
out.flush();
File bundleFile = new File( this.fullPathFileName );
long diskSize = bundleFile.length();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 21 2010
Added on Mar 24 2010
2 comments
633 views