Output Streams

The java.io.OutputStream class sends raw bytes of data to a target such as the console or a network server. Like InputStream, OutputStream is an abstract class. However, many methods in the class library are only specified to return OutputStream rather than the more specific subclass.

 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

The write() methods send raw bytes of data to whomever is listening to this stream.

Sometimes output streams are buffered by the operating system for performance. In other words, rather than writing each byte as it's written, the bytes are accumulated in a buffer ranging 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.

This is not the same as the buffering performed by a BufferedOutputStream. That buffering is handled by the Java runtime. This buffering is at the native OS level. However, a call to flush() should empty both buffers.

The close() method closes the stream and releases any resources associated with the stream. After the stream is closed, attempts to write to it throw IOExceptions.


Previous | Next | Top | Cafe au Lait

Copyright 1997-1999, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 22, 2006