简单的文件切割器(切割份数小于10份)

分割文件

1                 //主函数
2         File file=new File("");//文件的名称
3         File dir=new File("part");
4         if(!dir.exists()){
5             dir.mkdirs();
6         }
7         splitFile(file,dir);            
View Code

分割方法

public static void splitFile(File file,File dir) throws IOException{

        //用读取流关联源文件
        FileInputStream fis=new FileInputStream(file);
        //
        Properties prop=new Properties();
        //定义一个1M的缓冲区
        byte[] buf=new byte[SIZE];
        //创建目的
        FileOutputStream fos=null;
        int len=0;
        int count =0;
        while((len=fis.read(buf))!=-1){
            fos=new FileOutputStream(new File(dir,(count++)+".part"));
            fos.write(buf, 0, len);
            fos.close();
        }
        //将键写入到集合中
        prop.setProperty("partcount", count+"");
        prop.setProperty("filename", file.getName());
        //定义流
        fos=new FileOutputStream(new File(dir,count+".properties"));
        //将键写入到文本中
        prop.store(fos, "save file info");
        fis.close();
        fos.close();
    }
View Code

 

合并

 1 public static void main(String[] args) throws IOException {
 2         // TODO Auto-generated method stub
 3         File dir=new File("D:\\java练习\\day24\\part");
 4         mergeFile_2(dir);
 5     }
 6     public static void mergeFile_2(File dir) throws IOException {
 7         // TODO Auto-generated method stub
 8         //获取指定目录下配置文件的个数
 9         File[] files=dir.listFiles(new SuffixFileter(".properties"));
10         if(files.length!=1){
11             throw new RuntimeException(dir+",该目录下没有properties扩展名的文件");
12         }
13         //记录配置文件对象
14         File confile=files[0];
15         //获取该文件中的信息
16         Properties prop=new Properties();
17         FileInputStream fis=new FileInputStream(confile);
18         prop.load(fis);
19         String filename=prop.getProperty("filename");
20         int count =Integer.parseInt(prop.getProperty("partcount"));
21         //获取该目录下的所有碎片文件
22 
23         System.out.println(count);
24         
25         File[] partFiles=dir.listFiles(new SuffixFileter(".part"));
26         
27         if(partFiles.length!=count){
28             throw new RuntimeException("碎片文件不符合要求,个数不对!应该为"+count+"个");
29         }
30         //查看part文件
31         for(int x=0;x<partFiles.length;x++)
32         System.out.println(partFiles[x].toString());
33         
34         fis.close();
35         
36         ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
37         for(int x=0;x<partFiles.length;x++){
38             al.add(new FileInputStream(partFiles[x]));
39         }
40         Enumeration<FileInputStream> en=Collections.enumeration(al);
41         SequenceInputStream sis=new SequenceInputStream(en);
42         
43         FileOutputStream fos=new FileOutputStream(filename);
44         byte[] buf=new byte[1024];
45         int len=0;
46         while((len=sis.read(buf))!=-1){
47             fos.write(buf,0,len);
48         }
49         sis.close();
50         fos.close();
51     }
View Code

 

posted on 2013-12-09 19:04  ざ柒  阅读(147)  评论(0编辑  收藏  举报