RandomAccessFile分割合并文件

package com.dasenlin.spiltfile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import com.dasenlin.ioutil.IOUtil;

/**
 * 分割文件
 * @author Administrator
 *
 */
public class SpiltFile {

    private String filePath;//文件路径
    private int size;//块数   
    private String fileName;//文件名
    private long length;//文件大小
    private long blockSize;//分块大小
    private String destBlockPath;//分割后存放目录
    private List<String> blockPath;//所到路径

    public SpiltFile() {
        super();
        blockPath =new ArrayList<String>();
    }

    public SpiltFile(String filePath,String destBlockPath) {
        this(filePath,1024,destBlockPath);
    }
    //调用这个构造
    public SpiltFile(String filePath,long blockSize,String destBlockPath) {
        this();
        this.filePath=filePath;
        this.blockSize=blockSize;
        this.destBlockPath=destBlockPath;
        init();
    }

    public void init(){
        File src=null;
        if(null==filePath||!(src=new File(filePath)).exists()){
            return;
        }
        if(src.isDirectory()){
            return;
        }
        this.fileName=src.getName();
        //计算块数 实际大小与每块大小
        this.length =src.length();
        //修正大小
        if(this.blockSize>length){
           this.blockSize=length;
        }
        //计算块数
        size =(int) (Math.ceil(length*1.0/this.blockSize));
        initPathName();
    }
    //初始化路径名称
    private void initPathName(){
        for(int i=0;i<size;i++){
            this.blockPath.add(destBlockPath+"/"+this.fileName+".part"+i);
        }
    }
    /**
     * 文件的分割
     * @param destPath
     */
    public void split(String destPath){
        long begingPos =0;//起始点
        long actualBlockSize =blockSize;//实际大小
        for(int i=0;i<size;i++){
            if(i==size-1){
                actualBlockSize=this.length-begingPos;
            }
            splitDetail(i,begingPos,actualBlockSize);
            begingPos+=actualBlockSize;
        }
    }

    /**
     * 文件的分割
     * @param idx
     * @param beginPos
     * @param actualBlockSize
     */
    private void splitDetail(int idx,long beginPos,long actualBlockSize){
        //建立连接
        File src = new File(this.filePath);//原文件
        File dest = new File(this.blockPath.get(idx));//目标文件地址
        RandomAccessFile raf=null;//输入流
        BufferedOutputStream bos=null;//输出流
        try {
             raf = new RandomAccessFile(src,"r");
             bos = new BufferedOutputStream(new FileOutputStream(dest));
             raf.seek(beginPos);
             byte[]flush =new byte[1024];
             int len=0;
             while(-1!=(len=raf.read(flush))){
                 if(actualBlockSize-len>=0){
                     bos.write(flush, 0, len);
                     actualBlockSize-=len;//剩余量
                 }else{
                     bos.write(flush, 0,(int)actualBlockSize);
                     break;
                 }
             }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            IOUtil.ioclose(bos,raf);
        }
    }
    /**
     * 合并文件
     * @param srcPath
     */
    public void mergeFile(String destPath){
        File dest = new File(destPath); 
        BufferedOutputStream bos=null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(dest,true));//文件输出的追加
            BufferedInputStream bis=null;
            for(int i=0;i<blockPath.size();i++){
                bis = new BufferedInputStream(new FileInputStream(this.blockPath.get(i)));
                byte[]flush =new byte[1024];
                int len=0;
                while(-1!=(len=bis.read(flush))){
                    bos.write(flush, 0, len);
                }
                bos.flush();
                IOUtil.ioclose(bis);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            IOUtil.ioclose(bos);
        }
    }
//测试
    public static void main(String[] args) {
        SpiltFile split =new SpiltFile("D:/ccc/testcopydirectory/testiostream/xiaoce.txt",10,"D:/ccc/testcopydirectory/testiostream");
//      System.out.println(split.size);
//      split.split("D:/ccc/testcopydirectory/testiostream"); 
        split.mergeFile("D:/ccc/testcopydirectory/testiostream/bergexiaoce.txt");
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值