Stream io and io
Package org. richin. io. stream. util; 02 import java. io. bufferedInputStream; 03 import java. io. bufferedOutputStream; 04 import java. io. fileInputStream; 05 import java. io. fileOutputStream; 06 import java. io. IOException; 07 import java. io. inputStream; 08 import java. io. outputStream; 09/** 10 * streaming, write one input stream to multiple output streams at the same time 11 * @ author Administrator12 * 13 */14 public class TeeOutputStream extends OutputStream {15 private OutputStream out1; 16 private OutputStream out2; 17 public TeeOutputStream (OutputStream stream1, OutputStream stream2) 18 {19 // call the constructor of the parent class and input the stream to be filtered 20 // super (stream1); 21 out1 = stream1; 22 out2 = stream2; 23} 24 // rewrite the write Method 25 public void write (int B) throws IOException26 {27 out1.write (B); 28 out2.write (B ); 29} 30 // rewrite the write Method 31 public void write (byte [] data, int offset, int length) throws IOException32 {33 out1.write (data, offset, length ); 34 out2.write (data, offset, length); 35} 36 // rewrite the flush method 37 public void flush () throws IOException38 {39 out1.flush (); 40 out2.flush (); 41} 42 // override the close Method 43 public void close () throws IOException44 {45 out1.close (); 46 out2.close (); 47} 48 public static void copy (InputStream in, outputStream out) 49 throws IOException {50 // buffer stream 51 BufferedInputStream bin = new BufferedInputStream (in); 52 BufferedOutputStream bout = new BufferedOutputStream (out); 53 while (true) {54 int datum = bin. read (); 55 if (datum =-1) 56 break; 57 bout. write (datum); 58} 59 // refresh the buffer 60 bout. flush (); 61} 62 // main method 63 public static void main (String [] args) throws IOException {64 FileInputStream fin = new FileInputStream ("E: \ gzml \ gongzhens \ yaxi \ design draft \ express .rar "); 65 FileOutputStream fout1 = new FileOutputStream (" D:/Express .rar "); 66 FileOutputStream fout2 = new FileOutputStream ("e:/Express .rar"); 67 TeeOutputStream tout = new TeeOutputStream (fout1, fout2); 68 TeeOutputStream. copy (fin, tout); 69 fin. close (); 70 tout. close (); 71} 72}