使用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);
    }
}

猜你喜欢

转载自blog.csdn.net/u012843873/article/details/85006926