本帖最后由 一生一世 于 2012-6-26 12:28 编辑
/*
合并流
SequenceInputStream
视频在day20-16合并流
*/
import java.io.*;
import java.util.*;
class SequenceStreamDemo1{
public static void main(String args[])throws Exception{
SequenceStreamDemo1 ssd = new SequenceStreamDemo1();
ssd.mergeFile();
}
public void mergeFile() throws Exception{
String[] path = new String[]{"E:\\1.txt","E:\\2.txt","E:\\3.txt"};
Vector<FileInputStream> v = new Vector<FileInputStream>();
Enumeration<FileInputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("E:\\4.txt");
merge(path,v,sis,fos);
sis.close();
fos.close();
}
private void merge(String[] path, Vector<FileInputStream> v,
SequenceInputStream sis, FileOutputStream fos) throws Exception{
for(int i=0; i<path.length; i++){
v.add(new FileInputStream(path));
int len;
byte[] b = new byte[1024];
while ((len=sis.read(b))!=-1){//为什么这句话一直得不到执行呢??
fos.write(b,0,len);
/*if(!(b[len-2]==13 && b[len-1]==10)){
fos.write(13);
fos.write(10);
}*/
}
}
}
}
|
|