For reading from or writing to files PipedReader and PipedWriter Used to forward the output of one thread as the input to another thread StringReader and StringWriter For reading from or writing to strings in memory Table 2. SequenceInputStream Concatenates multiple input streams. ObjectInputStream and ObjectOutputStream Use for object serialization. LineNumberReader For reading while keep tracking of the line number. Each class performs a specific task on the data and forwards it to the next class in the chain.
Stream chaining can be very handy.
Figure 1 portrays chaining of three classes to convert raw data into compressed and encrypted data which is stored in a local file. This is how it works. CryptOutputStream encrypts the data prior to forwarding it to FileOutputStream which writes it out to a file. The result is a file which contains encrypted and compressed data. Type.java import java.io.