|Table of Contents|
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
java.ioare the abstract superclasses that define the behavior for sequential input and output streams in Java. Also included in
OutputStreamsubclasses that implement specific types of input and output streams. This lesson explains what each class in
java.iodoes, 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.iopackage, it does provide many practical examples on how to use the most popular classes in
Your First Encounter with I/O in JavaYou probably first encountered I/O streams in Java through the use of the standard output, standard error, and standard input streams managed by the
Overview of Input and Output StreamsThe
java.iopackage contains a full set of I/O streams; 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 StreamsThis section shows you the input and output stream pairs that derive directly from
OutputStreamand provides examples for their use.
Working with Filtered StreamsThe
java.iopackage 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.
BufferedOutputStreamare two such filtered streams that buffer data during reading and writing making the operation of the stream more efficient.
Working with Random Access FilesThe
java.ioimplement 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
java.ioimplements 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.
|Table of Contents|