Previous | Next | Trail Map | Essential Java Classes | Table of Contents


Reading and Writing

Often programs need to bring in information from an external source or send out information to an external destination. The information can be anywhere: in a file, on disk, somewhere on the network, in memory, or in another program. Also, it can be of any type: objects, characters, images, or sounds.

To bring in information, a program opens a stream on an information source (a file, memory, a socket) and reads the information serially, like this:

[PENDING: get figure from book]

Similarly, a program can send information to an external destination by opening a stream to a destination and writing the information out serially, like this:

[PENDING: get figure from book]

No matter where the information is coming from or going to and no matter what type of data is being read or written, the algorithms for reading and writing data is pretty much always the same.

[PENDING: insert table]

The java.io(in the API reference documentation) package contains a collection of stream classes that support these algorithms for reading and writing. These classes are divided into two class hierarchies based on the data type (either characters or bytes) on which they operate.

[PENDING: get figure from book]

However, it's often more convenient to group the classes based on their purpose rather than on the data type they read and write. Thus, we can cross-group the streams by whether they read from and write to data "sinks" or process the information as its being read or written.

[PENDING: get figure from book]

Overview of I/O Streams

This section describes each type of stream and shows the classes in java.io that implement them according to the division in the class hierarchy. Then, because most people think in terms of what they want to do rather than what they are doing it to, we provide two sections that show you how to use selected streams based on their purpose:

Object Serialization

Two of the byte streams, ObjectInputStream and ObjectOutputStream, are specialized streams that let you read and write objects. Reading and writing objects is a process known as object serialization. Object serialization has many uses, including remote method invocation (RMI). In addition to the object streams, java.io has other classes and interfaces that define the API to help classes perform serialization for its instances.

Working with Random Access Files

The character and byte streams are all sequential access streams. In contrast, RandomAccessFile lets you randomly access the contents of a file. This section talks about how to use random access files. It 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 filter streams because they can be used on random access files and on some sequential files.

And the Rest ...

This section briefly introduces the remaining classes in java.io.

The I/O Classes in Action

Many of the examples in the next trail, Custom Networking(in the Networking trail) use the I/O streams described in this lesson to read from and write to URLs, URLConnections, and Sockets.


Security consideration: Some I/O operations are subject to approval by the current security manager. The example programs contained in these lessons are standalone applications, which by default have no security manager. This code might not work in an applet depending on the in which browser or viewer it is running. See Security Restrictions(in the Writing Applets trail) for information about the security restrictions placed on applets.


Previous | Next | Trail Map | Essential Java Classes | Table of Contents