The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: I/O: Reading and Writing (but no 'rithmetic)

Working with Filter Streams

The java.io package provides a set of abstract classes that define and partially implement filter streams. A filter stream filters data as it's being read from or written to the stream. The filter streams are FilterInputStream (in the API reference documentation) or FilterOutputStream (in the API reference documentation), FilterInputStream (in the API reference documentation), and FilterOutputStream (in the API reference documentation). A filter stream is constructed on another stream (the underlying stream). The read method in a readable filter stream reads input from the underlying stream, filters it, and passes on the filtered data to the caller. The write method in a writable filter stream filters the data and then writes it to the underlying stream. The filtering done by the streams depends on the stream. Some streams buffer the data, some count data as it goes by, and others convert data to another form.

Most filter streams provided by the java.io package are subclasses of FilterInputStream and FilterOutputStream and are listed here:

The java.io package contains only one subclass of FilterReader: PushbackReader. So this section focuses on filter byte streams.

This section shows you how to use filter streams by presenting an example that uses a DataInputStream and a DataOutputStream. This section also covers how to subclass FilterInputStream and FilterOutputStream to create your own filter streams.

Using Filter Streams

To use a filter input or output stream, attach the filter stream to another input or output stream when you create it. For example, you can attach a filter stream to the standard input stream, as in the following code:
BufferedReader d = new BufferedReader(new DataInputStream(System.in));
String input;

while ((input = d.readLine()) != null) {
    ... //do something interesting here
}
Note that the readLine method has been deprecated in the DataInputStream; therefore we've wrapped it in a BufferedReader.

How to Use DataInputStream and DataOutputStream

This page provides and explains an example of using DataInputStream and DataOutputStream, two filtered streams that can read and write primitive data types.

Writing Your Own Filtered Streams

Many programmers find that they need to implement their own streams that filter or process data as it is being written to or read from the stream. Sometimes the processing is independent of the format of the data, such as counting various items in the stream, and sometimes the processing is directly related to the data itself or the format of the data, such as reading and writing data that is contained in rows and columns. Often, these programmers subclass FilterOutputStream and FilterInputStream to achieve their goals. This section describes an example of how to subclass FileInputStream and FilterOutputStream to create your own filtered streams.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.