Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Thejava.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 areFilterInputStream
orFilterOutputStream
,FilterInputStream
, andFilterOutputStream
. A filter stream is constructed on another stream (the underlying stream). Theread
method in a readable filter stream reads input from the underlying stream, filters it, and passes on the filtered data to the caller. Thewrite
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 ofFilterInputStream
andFilterOutputStream
and are listed here:The
DataInputStream
andDataOutputStream
BufferedInputStream
andBufferedOutputStream
LineNumberInputStream
PushbackInputStream
PrintStream
(This is an output stream.)java.io
package 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
DataInputStream
and aDataOutputStream
. This section also covers how to subclassFilterInputStream
andFilterOutputStream
to 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 }readLine
method has been deprecated in theDataInputStream
; therefore we've wrapped it in aBufferedReader
.
This page provides and explains an example of usingDataInputStream
andDataOutputStream
, 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 subclassFilterOutputStream
andFilterInputStream
to achieve their goals. This section describes an example of how to subclassFileInputStream
andFilterOutputStream
to create your own filtered streams.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.