序列流
SequenceInputStream ——对多个流进行合并
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
public class SequenceStream {
public static void main(String[] args) throws IOException {
FileInputStream file1 = new FileInputStream("file/file/file1.txt");
FileInputStream file2 = new FileInputStream("file/file/file2.txt");
FileInputStream file3 = new FileInputStream("file/file/file3.txt");
FileInputStream file4 = new FileInputStream("file/file/file4.txt");
Collection<FileInputStream> c = new ArrayList<FileInputStream>();
c.add(file1);
c.add(file2);
c.add(file3);
c.add(file4);
Enumeration<FileInputStream> e = Collections.enumeration(c);
SequenceInputStream ss = new SequenceInputStream(e);
//合并到f.txt
FileOutputStream fout = new FileOutputStream("file/file/f.txt");
byte b[] = new byte[2048];
int len = 0;
while ((len = ss.read(b)) != -1) {
fout.write(b, 0, len);
}
ss.close();
fout.close();
}
}
输出: