曾梦垚

导航

序列流实例

//序列流作用:可以将多个流串联到一起
public class Demo3 {

public static void main(String[] args) throws IOException {
testMerge2();

}

public static void testMerge2() throws IOException{
//1.获取文件
File file1 = new File("C:\\B\\a.txt");
File file2 = new File("C:\\B\\b.txt");
File file3 = new File("C:\\B\\c.txt");
File file4 = new File("C:\\B\\d.txt");

//2.创建通道
FileInputStream inputStream1 = new FileInputStream(file1);
FileInputStream inputStream2 = new FileInputStream(file2);
FileInputStream inputStream3 = new FileInputStream(file3);
FileOutputStream outputStream = new FileOutputStream(file4);

//3.创建一个Vector集合对象
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(inputStream1);
v.add(inputStream2);
v.add(inputStream3);

//4.获取迭代器
Enumeration<FileInputStream> e = v.elements();

//5.通过序列将三个流串起来
SequenceInputStream seq = new SequenceInputStream(e);
byte[] b = new byte[1024];

//6.读取数据
int length = 0;
while ((length = seq.read(b)) != -1) {
//写入数据
outputStream.write(b,0,length);
}
//7.关闭流
outputStream.close();
seq.close();


}

}

posted on 2016-12-10 11:56  曾梦垚  阅读(106)  评论(0编辑  收藏  举报