|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Thejava.iopackage 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 areFilterInputStreamor
FilterOutputStream,
FilterInputStream, and
FilterOutputStream. A filter stream is constructed on another stream (the underlying stream). The
readmethod in a readable filter stream reads input from the underlying stream, filters it, and passes on the filtered data to the caller. Thewritemethod 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.iopackage are subclasses ofFilterInputStreamandFilterOutputStreamand are listed here:The
DataInputStreamandDataOutputStreamBufferedInputStreamandBufferedOutputStreamLineNumberInputStreamPushbackInputStreamPrintStream(This is an output stream.)java.iopackage contains only one subclass ofFilterReader: 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
DataInputStreamand aDataOutputStream. This section also covers how to subclassFilterInputStreamandFilterOutputStreamto create your own 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:Note that theBufferedReader d = new BufferedReader(new DataInputStream(System.in)); String input; while ((input = d.readLine()) != null) { ... //do something interesting here }readLinemethod has been deprecated in theDataInputStream; therefore we've wrapped it in aBufferedReader.
This page provides and explains an example of usingDataInputStreamandDataOutputStream, two filtered streams that can read and write primitive data types.
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 subclassFilterOutputStreamandFilterInputStreamto achieve their goals. This section describes an example of how to subclassFileInputStreamandFilterOutputStreamto create your own filtered streams.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.