文件的分割和合并

学习IO流之处理文件文件

需求:

  • 对文件实现分割以及合并
  • 实现分割:
    • 通过输入流FileInputStream读取到文件
    • 指定文件大小读取
    • 将指定大小文件通过输出流FileOutputStream存储
  • 实现合并:
    • 通过读取原文件夹将读取到的文件进行存储
    • 对文件列表进行排序
    • 迭代进行写入操作实现合并
      详细看代码注释

代码实现 :


import java.io.*;
import java.util.Arrays;

/**
 * @author XiaoZhu
 */
public class FileCutter {

    /**
     * 处理分割参数
     *
     * @param str 原路径
     */
    public int split(String str) {
        File file = new File(str);
        if (!file.exists()) {
            throw new RuntimeException(str + ":路径无效!");
        }
        long size = 1024 * 1024 * 6;
        int i = str.lastIndexOf('.');
        String targetPath = str.substring(0, i);
        File directory = new File(targetPath);
        if (!directory.exists()) {
            directory.mkdir();
        }
        int splitCount = split(file, size, directory);
        System.out.println("文件分割成了" + splitCount + "个");
        return splitCount;
    }

    /**
     * 分割文件
     *
     * @param original  原路径
     * @param size      分割尺寸
     * @param directory 目标路径
     * @return fileCount 文件分割个数
     */
    public int split(final File original, final long size, final File directory) {
        int fileCount = 0;
        try (FileInputStream fis = new FileInputStream(original)) {
            int flag;
            byte[] bytes = new byte[(int) size];
            while ((flag = fis.read(bytes)) != -1) {
                File file = new File(directory, fileCount + ".FILE");
                try (FileOutputStream fos = new FileOutputStream(file)) {
                    fos.write(bytes, 0, flag);
                }
                fileCount++;
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return fileCount;
    }

    /**
     * 合并文件
     *
     * @param dir    源文件夹
     * @param target 目标文件夹
     * @return 返回文件合成后的大小
     */
    public long mergeFile(final File dir, final File target) {
        // 判断路径是否合法
        if (!dir.exists() || !target.exists()) {
            throw new RuntimeException(dir.exists() || target.exists() ? (dir + "路径不合法") : (target + "路径不合法"));
        }
        File[] files = dir.listFiles((file, name) -> name.endsWith(".FILE"));
        // 对文件进行排序
        Arrays.sort(files, (a, b) -> {
            int num1 = Integer.parseInt(a.getName().replace(".FILE", ""));
            int num2 = Integer.parseInt(b.getName().replace(".FILE", ""));
            return Integer.compare(num1, num2);
        });
        File targetFile = new File(target, "merge.mp4");
        try (FileOutputStream fos = new FileOutputStream(targetFile)) {
            // 迭代文件列表写入
            for (File file : files) {
                try (FileInputStream fis = new FileInputStream(file)) {
                    fis.transferTo(fos);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return targetFile.length();
    }
}

结果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值