关于文件的合并,视频里听的不是很明白,代码对于枚举这块不是很明白,求大神详细解释一下,多谢,代码如下:
public void sequence_demo() throws IOException{//合并
FileOutputStream op = new FileOutputStream("Sequence.zip");//定义合并后的目的地
FileInputStream fs = null;
//合并,需要使用类SequenceInputStream,经过查询API得知构造方法
//SequenceInputStream(Enumeration<? extends InputStream>e),参数是枚举类型,泛型是InputStream
//由于Enumeration枚举属于集合类,属于vactor类中,但是vactor效率不高,那么可以将ArrayList转换一下则:
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int i=1;i<=5;i++)//正常不应该写死,本测试知道已经将文件切割成5个人文件,所以此处为5
{
al.add(new FileInputStream(i+".part"));
}
//从这里开始不太理解---------begin
final Iterator<FileInputStream> it = al.iterator();
Enumeration<FileInputStream> et = new Enumeration<FileInputStream>(){
public boolean hasMoreElements(){
return it.hasNext();
}
public FileInputStream nextElement(){
return it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(et);
//从这里解释不理解--------end
byte[] bt = new byte[1024];
int len = 0;
while((len = sis.read(bt))!=-1){
op.write(bt, 0, len);
}
op.close();
sis.close();
} |