|
|
Input and Output Streams |
You attach a filtered stream to another stream to filter the data as it's read from or written to the original stream. Thejava.iopackage contains these filtered streams which are subclasses of eitherFilterInputStreamor
FilterOutputStream:
This section shows you how to use filtered streams through an example that uses a
DataInputStreamandDataOutputStreamBufferedInputStreamandBufferedOutputStreamLineNumberInputStreamPushbackInputStreamPrintStream(this is an output stream)DataInputStreamand aDataOutputStream. In addition, this section shows you how to write your own filtered streams.Using Filtered Streams
To use a filtered input or output stream, attach the filtered stream to another input or output stream. For example, you can attach aDataInputStreamto the standard input stream as in the following code:You might do this so that you can use the more convenientDataInputStream dis = new DataInputStream(System.in); String input; while ((input = dis.readLine()) != null) { . . . // do something interesting here }readXXXmethods, such asreadLine, implemented byDataInputStream.Using DataInputStream and DataOutputStream
This page provides and explains an example of usingDataInputStreamandDataOutputStream, two filtered streams that can read and write primitive Java 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 subclassFilterOutputStreamandFilterInputStreamto achieve their goals. This section describes an example of how to subclassFileInputStreamandFilterOutputStreamto create your own filtered streams.
|
|
Input and Output Streams |