IO流文件分割与合并

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cheidou123/article/details/83212490

文件分割与合并主要是知道要有这个思想,具体代码如下:

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/***
 * @author bincai
 * @email [email protected]
 */
public class No57_SplitFile {
    // 源头
    private File src;
    // 目的地
    private String destDir;
    // 所有分割后文件存储路径
    private List<String> destPaths;
    // 每块大小
    private int blockSize;
    // 块数
    private int size;

    public No57_SplitFile(String src, String destDir, int blockSize) {
        this.src = new File(src);
        this.destDir = destDir;
        this.blockSize = blockSize;
        this.destPaths = new ArrayList<>();
        init();
    }

    private void init() {
        long len = this.src.length();
        this.size = (int) Math.ceil(len * 1.0 / blockSize);
        for (int i = 0; i < size; i++) {
            this.destPaths.add(this.destDir + "/" +i + this.src.getName());
        }
    }


    public void split() throws Exception {
        // 总长度
        long len = src.length();
        // 起始位置
        int begin = 0;
        int actualSize = (int) (blockSize > len ? len : blockSize);
        for (int i = 0; i < size; i++) {
            begin = i * blockSize;
            if (i == size - 1) {
                actualSize = (int) len;
            } else {
                actualSize = blockSize;
                len -= actualSize;
            }
            splitDetails(i, begin, actualSize);
        }
    }

    private void splitDetails(int i, int begin, int actualSize) throws IOException {
        RandomAccessFile raf = null;
        RandomAccessFile raf2 = null;
        try {
            raf = new RandomAccessFile(this.src, "r");
            raf2 = new RandomAccessFile(this.destPaths.get(i), "rw");
            raf.seek(begin);
            byte[] flush = new byte[1024];
            int len = -1;
            while ((len = raf.read(flush)) != -1) {
                if (actualSize > len) {
                    raf2.write(flush, 0, len);
                    actualSize -= len;
                } else {
                    raf2.write(flush, 0, actualSize);
                    break;
                }
            }
        } finally {
            try {
                if (raf2 != null) {
                    raf2.close();
                }
            } catch (IOException ex) {

            }
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (IOException ex) {

            }
        }
    }

    public void merge(String destPath) throws IOException{
        //输出流
        OutputStream os = new BufferedOutputStream(new FileOutputStream(destPath,true));
        //输入流
        for(int i=0;i<destPaths.size();i++){
            InputStream is = new BufferedInputStream(new FileInputStream(destPaths.get(i)));
            byte[] flush = new byte[1024];
            int len = -1;
            while((len=is.read(flush))!=-1){
                os.write(flush,0,len);
            }
            os.flush();
            is.close();
        }
        os.close();
    }

    public static void main(String[] args) throws Exception{
     No57_SplitFile sf = new No57_SplitFile("timg.jpeg","splitimg",1024);
     sf.split();
     sf.merge("hei.jpeg");
    }
}

猜你喜欢

转载自blog.csdn.net/cheidou123/article/details/83212490