Multitarget Output Streams

As a final example, I present two slightly unusual filter output streams that direct their data to multiple underlying streams. The TeeOutputStream class in Example 6-6 has not one but two underlying streams. It does not modify the data that's written in any way; it merely writes that data on both of its underlying streams.

Example 6-6. The TeeOutputStream class

public class TeeOutputStream extends FilterOutputStream {
 private OutputStream out1;
 private OutputStream out2;
 public TeeOutputStream(OutputStream stream1, OutputStream stream2) {
 out1 = stream1;
 out2 = stream2;
 public void write(int b) throws IOException {
 public void write(byte[] data, int offset, int length)
 throws IOException {
 out1.write(data, offset, length);
 out2.write(data, offset, length);
 public void flush( ) throws IOException {
 out1.flush( );
 out2.flush( );
 public void close( ) throws IOException {
 out1.close( );
 out2.close( );

It would be possible to store one of the output streams in FilterOutputStream's protected out field and the other in a field in this class. However, it's simpler and cleaner to maintain the parallelism between the two streams by storing them both in the TeeOutputStream class.

Example 6-7 demonstrates how one might use this class to write a TeeCopier program that copies a file into two separate, new files.

Example 6-7. The TeeCopier program

public class TeeCopier {
 public static void main(String[] args) throws IOException {
 if (args.length != 3) {
 System.out.println("Usage: java TeeCopier infile outfile1 outfile2");
 FileInputStream fin = new FileInputStream(args[0]);
 FileOutputStream fout1 = new FileOutputStream(args[1]);
 FileOutputStream fout2 = new FileOutputStream(args[2]);
 TeeOutputStream tout = new TeeOutputStream(fout1, fout2);
 BufferedStreamCopier.copy(fin, tout);
 fin.close( );
 tout.close( );

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244 © 2008-2020.
If you may any questions please contact us: