flush : Java Glossary


a method common in I/O classes that effectively does all the normal file or stream close processing, but leaves the file open, or, if you prefer, conceptually closes then instantly reopens the file/stream. flush makes sure everything you have written so far is committed to the hard disk and the expanded file length is also committed to the disk directory, with the updated lastModified timestamp committed too.

If you crash the system later, you know at least that much is guaranteed to be there on disk waiting for you when you reboot.

When you flush a socket, it pushes every last byte you have written so far out the wire, even if it means sending a tiny packet.

When you flush a console, flush guarantees every byte you have written so far is displayed on screen.

Many constructors have an autofush boolean so that, when true, flushes automatically after every write.

flush is analogous to flushing a toilet. It clears the bowl/buffer of accumulated waste/bytes.

You don’t need to flush just prior to close because close handles the flushing.

The catch is flush does not necessarily get all your data on disk. You watch a file being written with flush, and as far as dir is concerned, it stays 0 bytes long. To force what amounts to a close/reopen to ensure your data is on disk in case of a crash, do this periodically.

Learning More

Oracle’s Javadoc on OutputStream.flush : available:
Oracle’s Javadoc on FileDescriptor.sync : available:
File I/O amanuensis: generates skeleton code using flush
Files & directories

This page is posted
on the web at:


Optional Replicator mirror
of mindprod.com
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number