JAVA_IO_文件的分割与合并(九)

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

RandomAccessFile

  • 该类的实例支持读取和写入随机访问文件。 随机访问文件的行为类似于存储在文件系统中的大量字节

  • extends Object
    
常用方法
seek(long pos)  设置文件指针偏移,从该文件的开头测量,发生下一次读取或写入。 

SequenceInputStream

  • extends InputStream
  • A SequenceInputStream表示其他输入流的逻辑级联。 它从一个有序的输入流集合开始,从第一个读取到文件的结尾,然后从第二个文件读取,依此类推,直到最后一个输入流达到文件的结尾。

构造方法摘要

SequenceInputStream(Enumeration<? extends InputStream> e) 
初始化新创建 SequenceInputStream通过记住参数,它必须是一个 Enumeration产生对象,它们的运行时类型是 InputStream 。  
SequenceInputStream(InputStream s1, InputStream s2) 
通过记住两个 SequenceInputStream来初始化新创建的SequenceInputStream,这些参数将按顺序读取,首先是 s1 ,然后是 s2 ,以提供要从此 SequenceInputStream读取的字节。  

文件的分割与合并

package com.hp.io;

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

/**
 * 进行文件的分割
 */
public class SpiltFileDemo {
    //文件路径
    private String filePath;
    //文件名称
    private String fileName;
    //
    private Long length;
    //块数
    private int size;
    //每块大小
    private Long blockSize;
    //每块的名称
    private List<String> blockFileName;

    public SpiltFileDemo() {
        blockFileName = new ArrayList<>();
    }

    public SpiltFileDemo(String filePath) {
        this(filePath, (long) 1024);
    }

    public SpiltFileDemo(String filePath, Long blockSize) {
        this();
        this.filePath = filePath;
        this.blockSize = blockSize;
        doinit();
    }

    /**
     * 初始化操作
     */
    public void doinit() {
        File file = null;
        //判断路径是否为空  文件是否真实存在
        if (null == filePath || !(file = new File(filePath)).exists()) {
            try {
                //自定义异常
                throw new NullFileException("当前文件为空/文件路径不存在");
            } catch (NullFileException e) {
                e.printStackTrace();
            }
        }
        //判断是否为文件夹
        if (file.isDirectory()) {
            try {
                throw new FolderFile("没有当前目录,因为是文件夹");
            } catch (FolderFile folderFile) {
                folderFile.printStackTrace();
            }
        }
        //文件名称
        fileName = file.getName();
        //修正每块大小
        this.length = file.length();
        //修正每块大小
        if (blockSize > length) {
            this.blockSize = length;
        }
        //确定块数
        size = (int) Math.ceil(length * 1.0 / this.blockSize);


    }

    public void setFileName(String destPath) {
        for (int i = 0; i < size; i++) {
            this.blockFileName.add(destPath + "/" + this.fileName + ".part" + i);
        }
    }


    public void spilt(String destPath) {
        //确认每块名称
        setFileName(destPath);

        //起始点
        int beginPos = 0;
        //实际大小
        Long actualSize = this.blockSize;
        for (int i = 0; i < size; i++) {
            if (i == size - 1) {
                actualSize = this.length - beginPos;
            }
            spiltDetail(i, beginPos, actualSize);
            beginPos += actualSize;
        }
    }

    /**
     * 文件分割
     */
    private void spiltDetail(int idx, int beginPos, Long actualSize) {
        //源文件
        File file = new File(this.filePath);
        //目标文件
        File file1 = new File(this.blockFileName.get(idx));
        RandomAccessFile randomAccessFile = null;
        BufferedOutputStream outputStream = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "r");
            outputStream = new BufferedOutputStream(new FileOutputStream(file1));
            randomAccessFile.seek(beginPos);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = randomAccessFile.read(bytes)) != -1) {
                if (actualSize - len >= 0) {
                    outputStream.write(bytes, 0, len);
                    actualSize -= len;
                } else {
                    Long len1 = actualSize;
                    outputStream.write(bytes, 0, actualSize.intValue());
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 文件的合并
     *
     * @param destPath
     */
    private void fileMerge(String destPath) throws IOException {
        SequenceInputStream sequenceInputStream = null;
        BufferedOutputStream outputStream = null;
        File dest = new File(destPath);
        Vector<InputStream> vector = new Vector<>();
        for (int i = 0; i < this.blockFileName.size(); i++) {
            vector.add(new BufferedInputStream(new FileInputStream(new File(this.blockFileName.get(i)))));
        }
        sequenceInputStream = new SequenceInputStream(vector.elements());
        outputStream = new BufferedOutputStream(new FileOutputStream(dest,true));
        byte[] bytes = new byte[1024];
        int len = 0;
        while (-1 != (len = sequenceInputStream.read(bytes))) {
            outputStream.write(bytes, 0, len);
            System.out.println(new String(bytes,0,len));
        }

        outputStream.flush();
        sequenceInputStream.close();
        outputStream.close();

    }

    public static void main(String[] args) {
        SpiltFileDemo fileDemo = new SpiltFileDemo("C:/Users/晓电脑/Desktop/a.txt", 50L);

        System.out.println(fileDemo.size);
        fileDemo.spilt("C:/Users/晓电脑/Desktop");
        try {
            fileDemo.fileMerge("C:/Users/晓电脑/Desktop/arr.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

其中里面有我自己自定义的异常,这里我只写一个

NullFileException.java

package com.hp.io;

public class NullFileException extends Exception {
    public NullFileException() {
        super();
    }

    public NullFileException(String message) {
        super(message);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40646143/article/details/84477226