자바 로 파일 분할 및 병합

7575 단어 자바
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 al = new ArrayList();
        //      
        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);
    }
}

좋은 웹페이지 즐겨찾기