今天看了毕老师的视频,写了一个文件合并的demo,可是文件没办法打开,请大神来帮我看下。。- import java.io.*;
- import java.util.*;
- public class SplitFileDemo {
- //main 函数
- public static void main(String[] args) throws IOException{
- // splitFile();
- mergeFile();
- }
- public static void mergeFile() throws IOException{
- //用arraylist集合存放输入流
- ArrayList<FileInputStream> al = new ArrayList<FileInputStream> ();
- //将3个碎片文件放入al中
- for(int i = 1; i<=3;i++){
- al.add(new FileInputStream("e:\\split\\"+i+".part"));
- }
- //定义一个迭代器 it
- final Iterator<FileInputStream> it = al.iterator();
- //用Enumeration 对象对输入流进行枚举
- Enumeration<FileInputStream> en = new Enumeration<FileInputStream>(){
- public boolean hasMoreElements(){
- return it.hasNext();
- }
- public FileInputStream nextElement(){
- return it.next();
- }
- };
- //对输入流进行合并
- SequenceInputStream sis = new SequenceInputStream(en);
- //定义输出流对象和 输出文件路径
- FileOutputStream fos = new FileOutputStream("e:\\split\\a.bmp");
-
- byte[] buf = new byte[1024];
- int len = 0;
- //输出流进行写文件操作
- while((len = sis.read())!=-1){
- fos.write(buf,0,len);
- }
- //流关闭
- fos.close();
- sis.close();
- }
- }
复制代码
|