Java Input/Output  «Prev  Next»

Lesson 5Learn how output streams are used to write data.
ObjectiveJava Output streams

How Java Output Streams write Data

The OutputStream class serves as the base class for all output streams, and supports the following commonly used methods:

Which functions does the OutputStream Class in Java fulfill?

The java.io.OutputStream class is an abstract class in Java that provides a basic framework for writing data to an output destination. Some of the functions that the OutputStream class fulfills include:
  1. Writing binary data: The OutputStream class provides methods for writing primitive data types, such as bytes, ints, and floats, to an output destination.
  2. Buffering: The OutputStream class provides a default implementation of buffering, which can improve performance by reducing the number of I/O operations required to write data to the output destination.
  3. Flushing: The OutputStream class provides a method for flushing the output buffer, which can be useful when it is important to ensure that data is written to the output destination as soon as possible.
  4. Closing: The OutputStream class provides a method for closing the output stream, which can be useful when it is necessary to release any resources that may be associated with the output stream, such as file handles or network connections.

Overall, the OutputStream class in Java provides a simple and flexible way to write data to an output destination, and is the foundation for many of the more specialized output stream classes in the Java I/O framework.


write()writes a byte or series of bytes of data
flush()forces data to be written if it hasn't been already

Buffered I/O

Java supports buffered I/O, which means that data is written to a temporary buffer instead of to the stream. When the buffer is full, all of the data in the buffer is written to the stream.
Buffered I/O is more efficient than non-buffered I/O since data is transferred to and from a physical device in large blocks.
  1. The flush() method in the OutputStream class is used to force a buffer to be written to an output device.
  2. DataOutput is the counterpart to the DataInput interface, and is used to output data in a primitive Java data format.
  3. The DataOutput interface provides methods such as writeBoolean(), writeInt(), and writeFloat(). The DataOutputStream class implements the DataOutput interface.
  4. The Writer class is the counterpart to Reader, and provides an internationalized alternative to the OutputStream class.
  5. The Writer class is designed similarly to OutputStream and supports a similar set of methods.

Note: Although they are named differently from the Java 1.0 stream classes, the Reader and Writer classes are still considered streams. So, the term "stream" applies to both sets of classes.

IO Streams Java - Quiz

Click the Quiz link below to test your knowledge of Java I/O.
IO Streams Java - Quiz