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();
- }
- }
复制代码 |
|