Many output streams buffer writes to improve performance. Rather than sending each byte to its destination as it's written, the bytes are accumulated in a memory buffer ranging in size from several bytes to several thousand bytes. When the buffer fills up, all the data is sent at once. The flush( ) method forces the data to be written whether or not the buffer is full:
public void flush( ) throws IOException
This is not the same as any buffering performed by the operating system or the hardware. These buffers will not be emptied by a call to flush( ). (Then sync( ) method in the FileDescriptor class, discussed in Chapter 17, can sometimes empty these buffers.)
If you use a stream for only a short time, you don't need to flush it explicitly. It should flush automatically when the stream is closed. This should happen when the program exits or when the close( ) method is invoked. You flush an output stream explicitly only if you want to make sure data is sent before you're through with the stream. For example, a program that sends bursts of data across the network periodically should flush after each burst of data is written to the stream.
Flushing is often important when you're trying to debug a crashing program. All streams flush automatically when their buffers fill up, and all streams should be flushed when a program terminates normally. If a program terminates abnormally, however, buffers may not get flushed. In this case, unless there is an explicit call to flush( ) after each write, you can't be sure the data that appears in the output indicates the point at which the program crashed. In fact, the program may have continued to run for some time past that point before it crashed.
System.out, System.err, and some (but not all) other print streams automatically flush after each call to println( ) and after each time a new line character (' ') appears in the string being written. You can enable or disable auto-flushing in the PrintStream constructor.
2.4.1. The Flushable Interface
Java 5 added a Flushable interface that the OutputStream class implements:
package java.io; public interface Flushable { void flush( ) throws IOException; }
Formatter and various other things that can be flushed also implement this interface. I've never figured out the use case that justifies this extra interface either, but it's there if for some reason you want to write a method that accepts only objects that can be flushed as arguments, or some such.
Basic I/O
Introducing I/O
Output Streams
Input Streams
Data Sources
File Streams
Network Streams
Filter Streams
Filter Streams
Print Streams
Data Streams
Streams in Memory
Compressing Streams
JAR Archives
Cryptographic Streams
Object Serialization
New I/O
Buffers
Channels
Nonblocking I/O
The File System
Working with Files
File Dialogs and Choosers
Text
Character Sets and Unicode
Readers and Writers
Formatted I/O with java.text
Devices
The Java Communications API
USB
The J2ME Generic Connection Framework
Bluetooth
Character Sets