![]() ![]() ![]() ![]() |
Input and Output Streams |
Filtered streams process data as its being written to or read from the stream. The java.io package implements these filtered streams:This section shows you how to use filtered streams through an examle that uses a DataInputStream and a DataOutputStream. In addition, this section shows you how to write your own filtered streams.
- DataInputStream and DataOutputStream
- BufferedInputStream and BufferedOutputStream
- LineNumberInputStream
- PushbackInputStream
- PrintStream (this is an output stream)
Using Filtered Streams
To use a filtered input (output) stream, you attach the filtered stream to another input (output) stream. For example, you can attach a DataInputStream to the standard input stream like this:You might do this so that you can use the more convenientDataInputStream dis = new DataInputStream(System.in.read()); String input; while ((input = dis.readLine()) != null) { . . . // do something interesting here }readXXX()
methods, such asreadLine()
, implemented by DataInputStream.Using DataInputStream and DataInputStream
This page provides and explains an example of using DataInputStream and DataOutputStream, 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 the 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.Filtering Random Access Files
The filtered streams in java.io all inherit from InputStream or OutputStream which implement sequential access files. Thus, your filtered streams will also be sequential access files. Using Random Access Files later in this lesson shows you how to re-write the example from Writing Your Own Filtered Streams so that it works on a RandomAccessFile as well as on a DataInputStream or a DataOutputStream.See Also
java.io.FilterOutputStream
java.io.FilterInputStream
![]() ![]() ![]() ![]() |
Input and Output Streams |