黑马程序员技术交流社区
标题:
IO合并文件 使用ArrayList 复写Enumeration 内的方法体
[打印本页]
作者:
fmi110
时间:
2015-8-8 14:35
标题:
IO合并文件 使用ArrayList 复写Enumeration 内的方法体
IO
class
{
public static void main(String[] args)
{
merge();
System.out.println("Hello World!");
}
public static void merge() throws IOException
{
//集合接受流
ArrayList<FileInputStream> ar = new ArrayList<FileInputStream>();
//添加要合并的文件
ar.add(new FileInputStream("d:\\1.part"));
ar.add(new FileInputStream("d:\\2.part"));
//创建输出流
FileOutputStream fos = null;
final Iterator<FileInputStream> it = ar.iterator();//为保证传递给内部类后数据的一致性,用final修饰
//复写枚举,并传递给SequenceInputStream
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
public boolean hasMoreElements(){
return it.hasNext();
}
public FileInputStream nextElement(){
return it.next();
}
};
SequenceInputStream sis = new SequenceInputStream(en);
//缓冲
byte[] buf = new byte[1024*1024];
int len;
fos = new FileOutputStream("d:\\mergeFile.jpg");
while((len=sis.read(buf))!=-1){
fos.write(buf,0,len);
fos.flush();
}
fos.close();
sis.close();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2