Java Streams   «Prev  Next»

Lesson 6Flushing and closing
ObjectiveExamine when to flush and close streams.

Java IO Flushing and Closing

Many operating systems buffer writes to improve performance. In other words, rather than writing each byte as it is written, the bytes are accumulated in a buffer ranging in size from several bytes to several thousand bytes. Then, when the buffer fills up, all the data is written at once.
The flush() method forces the data to be written whether or not the buffer is full.
If you only use a stream for a short time, you do not need to flush it explicitly. It should be flushed when the stream is closed. Of course this requires you to actually call the close() method instead, but generally you do not need to call both close() and flush().

Flushing and Closing Streams
You only need to explicitly flush an output stream if for some reason you need to make sure data is sent before you are through with the stream. For example, a program that sends a burst of data across the network periodically should flush after each burst of data is written to the stream. Flushing is often important when you are trying to debug a crashing program.
System.out, System.err, and some (but not all) other print streams automatically flush after each call to println() and after each time a newline character ('\n') appears in the string being written. Whether autoflushing is enabled can be set in the PrintStream constructor.

OutputStream Class

The java.io.OutputStream class declares the three basic methods you need to write bytes of data onto a stream. It also has methods for closing and flushing streams.
public abstract void write(int b) throws IOException
public void write(byte[] data) throws IOException
public void write(byte[] data, int offset, int length) throws IOException
public void flush() throws IOException
public void close() throws IOException

OutputStream is an abstract class. Subclasses provide implementations of the abstract write(int b) method. They may also override the four nonabstract methods. For example, the FileOutputStream class overrides all five methods with native methods that know how to write bytes into files on the host platform. Although OutputStream is abstract, often you only need to know that the object you have is an OutputStream ; the more specific subclass of OutputStream is hidden from you. For example, the getOutputStream() method of java.net.URLConnection has the signature:
public OutputStream getOutputStream() throws IOException

Depending on the type of URL associated with this URLConnection object, the actual class of the output stream that's returned may be a
  1. sun.net.TelnetOutputStream,
  2. sun.net.smtp.SmtpPrintStream,
  3. sun.net.www.http.KeepAliveStream.

All you know as a programmer, and all you need to know, is that the object returned is in fact some instance of OutputStream. That's why the detailed classes that handle particular kinds of connections are hidden inside the sun packages.
Furthermore, even when working with subclasses whose types you know, you still need to be able to use the methods inherited from OutputStream. And since methods that are inherited are not included in the online documentation, it's important to remember that they're there. For example, the java.io.DataOutputStream class does not declare a close() method, but you can still call the one it inherits from its superclass.

Writing Java Streams - Quiz

Click on the Quiz link below to take a brief multiple-choice quiz on writing streams.
Writing Java Streams - Quiz