java提高篇(13)--SequenceInputStream ——对多个流进行合并

来源:互联网 发布:mac可以玩qq堂吗 编辑:程序博客网 时间:2024/05/17 03:01
[java] view plain copy
  1. package ThreadIO2_1.IO.Sequence;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.SequenceInputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.Collections;  
  9. import java.util.Enumeration;  
  10.   
  11. public class SequenceInputStreamDemo {  
  12.   
  13.     public static void main(String[] args) throws IOException {  
  14.         FileInputStream fis1 = new FileInputStream("files\\seq1.txt");  
  15.         FileInputStream fis2 = new FileInputStream("files\\seq2.txt");  
  16.         FileInputStream fis3 = new FileInputStream("files\\seq3.txt");  
  17.         ArrayList<FileInputStream> c = new ArrayList<FileInputStream>();  
  18.         c.add(fis1);  
  19.         c.add(fis2);  
  20.         c.add(fis3);  
  21.         Enumeration<FileInputStream> e = Collections.enumeration(c);  
  22.         SequenceInputStream sis = new SequenceInputStream(e);  
  23.           
  24.         //创建输出流---要把前三个文件的内容读出来并且合并到seq4.txt;  
  25.         FileOutputStream fos=new FileOutputStream("files\\seq4.txt");  
  26.         int len=0;  
  27.         byte buf[] = new byte[10];  
  28.         while((len=sis.read(buf))!=-1){  
  29.             fos.write(buf, 0, len);  
  30.         }  
  31.         fos.close();  
  32.         sis.close();  
  33.   
  34.     }  
  35.   
  36. }  
[java] view plain copy
  1.   
[java] view plain copy
  1. <span style="font-size:18px;color:#ff0000;">总结:将多个流进行逻辑串联(合并变成一个流,操作起来很方便,因为多个源变成了一个源) </span>  
阅读全文
0 0