Input and Output Streams

Many Java programs need to read or write data. In this tutorial, you have seen several example programs that read and write data. For example, the sample program in the Java Nuts and Bolts section (in the Writing Java Programs journal) reads user input and writes the data to the display, while the All About Sockets lesson (in the Journal Network Connections ”), which you will read later, contains several examples that read and write data over a network using sockets. All of these sample programs read and write to streams.

Definition: A stream is a continuous sequence of characters.

Your program can receive input from a data source by reading sequence characters from a stream attached to the source. Your program can generate output by typing the order of the characters in the output stream attached to the destination. The Java IDE includes the java.io package, which includes a set of input and output streams that your programs can use to read and write data.

Input stream and output stream classes in java.io are abstract superclasses that explain the behavior of input and output streams, respectively, in Java. Java.io also includes several subclasses of input streams and output streams that implement specific types of input and output streams. This tutorial explains what each class in java.io does, how you decide which ones to use, how to use them, and how you can sub- take a class to write your own stream classes.

Although this tutorial does not provide examples for all types of input and output streams available in the java.io package, it does provide several practical examples of using the most popular classes in java.io.

Your first encounter with Java I / O

You may have encountered I / O streams in Java before, thanks to the standard output, standard error, and the use of standard input streams managed by the system.

Overview of input and output streams.

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

Using input and output streams

This section shows pairs of input and output streams that derive directly from InputStream and OutputStream, and provides examples of 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 that filter data as it is read from or written to the stream. BufferedInputStream and BufferedOutputStream are two such filtered streams that buffer data as it reads and writes, making the stream more efficient.

Working with random access files

In java input output overview, the InputStream and OutputStream subclasses produce serial files – files that need to be processed from start to finish. A random access file gives your programs the ability to access data in a file in an inconsistent (or random) order. RandomAccessFile in java.io implements a file that you can access in an inconsistent manner.

Security considerations: Input and output on the local file system are subject to approval by the current security manager. The sample programs contained in these lessons are stand-alone applications that do not have a Security Manager by default. If you try to use this code in applets, it may not work depending on the browser or viewer in which they are running. See “Understanding the Features and Limitations of Applets” (in the Writing Applet Journal) for information on the security restrictions imposed on applets.