| Table of Contents

# Input and Output Streams

> Many Java programs need to read or write data. 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 reads data entered by the user and writes data to the display, and the All About Sockets lesson which you'll read later contains several examples that read and write data over the network using sockets. These example programs all read from and write to streams.

> Definition: A stream is a flowing sequence of characters.

> Your program can get input from a data source by reading a sequence characters from a stream attached to the source. Your program can produce output by writing a sequence of characters to an output stream attached to a destination. 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. The InputStream and OutputStream classes in java.io are the abstract superclasses that define the behaviour for sequential input and output streams in Java. Also included in java.io are several InputStream and OutputStream subclasses that implement specific types of input and output streams. This lesson explains what each class in java.io does, how to decide which ones to use, how to use them, and how to subclass them to write your own stream classes.

> 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 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 section 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

> > The java.io package contains several I/O streams that belong to a set of streams known as filtered streams which filter data as it's being read from or written to the stream. BufferedInputStream and BufferedOutputStream are two such filtered streams that buffer data during reading and writing making the operation of the stream more efficient.

> #### Working with Random Access Files

> > The InputStream and OutputStream subclasses in java.io implement sequential access files--files that must be processed in order from beginning to end. A random access file provides your programs with the ability to access data in the file in non-sequential (or random) order. The RandomAccessFile in java.io implements a file that you can access in non-sequential order.

> Security consideration: Input and output on the local file system are subject to approval by the current security manager. The example programs contained in these lessons are stand alone applications, which by default have no security manager. If you attempt to use this code in applets it might not work depending on the browser or viewer they are running in. See Understanding Applet Capabilities and Restrictions for information about the security restrictions placed on applets.

> #### See Also

> > java.io

| Table of Contents