合并流合并多个文件 – 恵玩科技

package IO.com.lxgyx_06;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

public class SequenceInputStreamDemo2 {
    public static void main(String[] args) throws IOException {
        //复制3个以上的文件
        Vector<InputStream> v=new Vector<InputStream>();
        InputStream is1=new FileInputStream("test\\pw.txt");
        InputStream is2=new FileInputStream("test\\pw3.java");
        InputStream is3=new FileInputStream("test\\pw2.txt");

        v.add(is1);
        v.add(is2);
        v.add(is3);

        Enumeration<InputStream> en=v.elements();//集合元素枚举
        SequenceInputStream sis=new SequenceInputStream(en);
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("test\\pw4.txt"));

        byte[] bys=new byte[1024];
        int len=0;
        while ((len=sis.read(bys))!=-1){
            bos.write(bys,0,len);
        }

        sis.close();
        bos.close();
    }
}

 

打赏作者