Previous | Next | Trail Map | Writing Java Programs | Table of Contents


Input and Output Streams

Many Java programs that you write need to read data from some input source or write data to some output source. You have seen several examples in this tutorial of programs that read and write data. For example, the example program featured in The Nuts and Bolts of the Java Language(in the Writing Java Programs trail)reads data entered by the user and writes data to the display, and the All about Sockets(in the Networking trail)lesson which you'll read later contains several examples that read and write data over the network using sockets.

The Java development environment includes a package, java.io, that contains a set of input and output streams that your programs can use to read and write data. This lesson explains what each class in the java.io packages does, how to decide which one to use, how to use them, and how to subclass them to write your own. While this lesson does not have an example for every type of input and output stream available in the java.io package, it does provide many practical examples on how to use the most popular classes in java.io.

Your First Encounter with I/O in Java

You probably first encountered I/O streams in Java through the use of the standard output, standard error, and standard input streams managed by the System class.

Overview of java.io's Input and Output Streams

Your Java programs use input streams to read data from some input source and output streams to write data to some output source. Input and output sources can be anything that can contain data: a file, a string, or memory.

Using Input and Output Streams

This page shows you the input and output stream pairs that derive directly from InputStream and OutputStream and provides examples for their use.

Working with Filtered Streams

This page describes what filtered streams are, how to use them, and how to write your own.

Working with Random Access Files

The java.io.RandomAccessFile implements a random access file that allows you to seek to different locations within the file during reading and writing. This page also provides a special section that shows you how to write filters for objects that implement the DataInput and DataOutput interfaces. Filters implemented in this fashion are more flexible than regular filtered streams because they can be used on random access files and on some sequential files.

See Also

java.io


Previous | Next | Trail Map | Writing Java Programs | Table of Contents