使用Java对文件进行分割与合并

package com.io.splitfile.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;

//需求:对文件进行切割和合并,生成相应的配置文件,包括碎片个数partcount,源文件名称filename。
public class SplitMergeFile {
    private static final int SIZE = 1024*1024;
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\test\\a.avi");
        File dir = new File("C:\\Users\\Administrator\\Desktop\\test\\parties");
//      splitFile(file);
        String fileFormat = "avi";
        mergeFile(dir,fileFormat);
    }
    public static void splitFile(File file) throws IOException{
        //取出文件格式,以便生成相应格式的文件
        String fileFormat = file.getName().substring( file.getName().lastIndexOf('.'));
        //取出文件的父目录
        File parentDir =  file.getParentFile();
        //生成文件的目的地
        File destFile = new File(parentDir,"parties");
        if(!destFile.exists()){
            destFile.mkdirs();
        }
        FileInputStream fis = new FileInputStream( file);
        byte[] buf = new byte[SIZE];
        int len = 0;
        int count = 1;
        FileOutputStream fos = null;
        while((len = fis.read(buf)) != -1){
            fos = new FileOutputStream(new File(destFile,(count++) + fileFormat));
            fos.write(buf, 0, len);
            fos.close();
        }
        fis.close();
        Properties prop = new Properties();
        prop.setProperty("partcount",(count-1)+"");
        prop.setProperty("filename", file.getName());
        fos = new FileOutputStream(new File(destFile,count+".properties"));
        prop.store(fos, "FileInformation");
        fos.close();
    }
    public static void mergeFile(File dir,String fileFormat) throws IOException{
        File[] files = dir.listFiles(new SuffixFilter(".properties"));
        if(files.length != 1)
            throw new NoFileException("文件不存在或文件不唯一");
        File confile = files[0];
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream(confile);
        prop.load(fis);
        int count = Integer.parseInt(prop.getProperty("partcount"));
        String filename = prop.getProperty("filename");
        ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
        //获取碎片文件
        File[] partFiles = dir.listFiles(new SuffixFilter("." + fileFormat));
        if(count != partFiles.length)
            throw new FileCountException("碎片文件个数不符,应为"+count+"个");
        for(int i = 0; i < partFiles.length; i++ ){
            al.add(new FileInputStream(partFiles[i]));
        }
        Enumeration en = Collections.enumeration(al);
        SequenceInputStream sis = new SequenceInputStream(en);
        FileOutputStream fos = new FileOutputStream(new File(dir,filename));
        byte[] buf = new byte[SIZE];
        int len = 0;
        while((len = sis.read(buf)) != -1){
            fos.write(buf,0,len);
        }
        fos.close();
        sis.close();
    }


}
class NoFileException extends RuntimeException{
    NoFileException(){
        super();
    }
    NoFileException(String msg){
        super(msg);
    }
}
class FileCountException extends RuntimeException{
    FileCountException(){
        super();
    }
    FileCountException(String msg){
        super(msg);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值