开箱即用IO流实现文件分块合并

该文章介绍了如何使用Java的IO流和RandomAccessFile类来实现文件的分块和合并,主要应用于断点上传场景。首先,文章展示了如何将一个大文件按1MB的块大小进行分割,并保存到指定目录。接着,详细说明了如何按照块编号顺序将这些分块文件重新合并成原始文件。
摘要由CSDN通过智能技术生成


在这里插入图片描述

断点上传文件的前置知识,用io流实现文件分块与合并。
分块示例

一、文件分块

    /**
     * 文件分块
     *
     * @param sourceFile 源文件
     * @param targetDir  分块文件存放目录
     */
    public static void Chunk(String sourceFile, String targetDir) {
        //        准备文件
        File file = new File(sourceFile);
        if (!file.exists()) {
            System.out.println("源文件不存在");
            return;
        }

        //准备输入流读取文件,并且分块写入到指定目录
        RandomAccessFile randomAccessFile = null;
        try {
//            随机流,用于读取文件到内存,并且写入文件到磁盘
            randomAccessFile = new RandomAccessFile(file, "rw");
            //        分块大小 此处为1M
            long chunkSize = 1024 * 1024 * 1;

//        每次读取的字节数
            byte[] bytes = new byte[1024];

//        分块数   向上取整
            long ceil = (long) Math.ceil(randomAccessFile.length() * 1.0 / chunkSize);
//          分块文件放在指定目录
            File dir = new File(targetDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            int len = -1;
            for (int i = 0; i < ceil; i++) {
                File temp = new File(targetDir + "\\" + i);
                RandomAccessFile randomAccessFile1 = new RandomAccessFile(temp, "rw");
                while ((len = randomAccessFile.read(bytes)) != -1) {
                    randomAccessFile1.write(bytes, 0, len);
                    if (temp.length() >= chunkSize) {
                        break;
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                    System.out.println("分块成功");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

二、文件合并

    /**
     * @param sourceDir 分块文件目录
     * @param fileName  合并文件名,需要带上后缀
     */
    public static void Merge(String sourceDir, String fileName) {
//        找到源文件路径,循环所有的分块文件,输出到一个文件中
        File file = new File(sourceDir);
        if (!file.exists()) {
            System.out.println("目录不存在");
            return;
        }
        File[] files = file.listFiles();
        if (files.length == 0) {
            System.out.println("目录中没有文件");
        }

        RandomAccessFile randomAccessFile = null;
        try {
//            合并的文件放在分块目录中
            randomAccessFile = new RandomAccessFile(sourceDir + "\\" + fileName, "rw");
            //        确保合并的顺序   分块是0-n,要确保合并顺序
            List<File> list = Arrays.asList(files);
            Collections.sort(list, (a, b) -> Integer.valueOf(a.getName()) - Integer.valueOf(b.getName()));
            RandomAccessFile randomAccessFile1 = null;
            for (File temp : list) {
                randomAccessFile1 = new RandomAccessFile(temp, "rw");
                int len = -1;
                byte[] bytes = new byte[1024];
                while ((len = randomAccessFile1.read(bytes)) != -1) {
                    randomAccessFile.write(bytes, 0, len);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                randomAccessFile.close();
                System.out.println("合并成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值