java 文件分割与合并

java 文件分割与合并



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

public class FileDivisionMergeDemo {
    /**
     * 文件分割
     *
     * @param url      文件
     * @param cutSize  每个文件的大小
     * @param fileName 文件名称
     * @param address  分割到的地址
     * @param suffix   后缀
     */
    private static void division(String url, long cutSize, String fileName, String address, String suffix) {
        File targetFile = new File(url);
//        判断文件是否为空
        if (targetFile == null || !targetFile.exists()) return;
        if (fileName == null) {
            String[] strArray = targetFile.getName().split("\\.");
            Arrays.copyOf(strArray, strArray.length - 1);
            fileName = String.join("", strArray);
        }
        if (address == null) {
            String a = targetFile.getAbsolutePath();
            address = a.split(targetFile.getName())[0] + fileName + "\\";
        }
        if (suffix == null) {
            suffix = ".tmp";
        }
        File file = new File(address);
        if (!file.exists()) {
            file.mkdirs();
        }
        //        分成几个文件

        int num = (int) ((targetFile.length() % cutSize == 0) ? (targetFile.length() / cutSize) : ((targetFile.length() / cutSize) + 1));
        try {
//            获取文件流 并将文件进入缓存流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(targetFile));
            BufferedOutputStream out = null;
            int count = 0;
            int len = -1;
            byte[] bytes = null;
            for (int i = 0; i < num; i++) {
                out = new BufferedOutputStream(new FileOutputStream(address + fileName + i + suffix));

                //  每个byte数组最多1024 求要处理几次
                if (cutSize > 1024) {
                    count = (int) (cutSize / 1024);
                    bytes = new byte[1024];
                } else {
                    count = 1;
                    bytes = new byte[(int) cutSize];
                }

                while (count > 0 && (len = bis.read(bytes)) != -1) {
                    out.write(bytes, 0, len);
                    out.flush();
                    count--;
                }
                if ((cutSize % 1024) != 0) {
                    bytes = new byte[(int) (cutSize % 1024)];
                    len = bis.read(bytes);
                    out.write(bytes, 0, len);
                    out.flush();
                    out.close();
                }
                out.close();
            }

            System.out.println("分割完成");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 合并文件
     *
     * @param url      文件夹路径
     * @param name     要合成的文件名称
     * @param address  合成后需要放到的文件路径
     * @param isDelete 是否删除缓存文件
     */
    private static void merge(String url, String name, String address, Boolean isDelete) {
        File file = new File(url);
        if (!file.exists()) {
            System.out.println("文件夹地址不存在");
            return;
        }
        if (!file.canRead()) {
            System.out.println("文件地址不可读");
            return;
        }
        if (new File(url + name).exists()) {
            System.out.println("目标文件已存在");
            return;
        }
        if (isDelete == null) {
            isDelete = true;
        }
        File[] files = file.listFiles();
        System.out.println(files.length);
        try {
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(url + "/../" + name));
            for (File f : files) {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
                byte[] bytes = new byte[1024];
                int len = -1;
                while ((len = bis.read(bytes)) != -1) {
                    out.write(bytes, 0, len);
                    out.flush();
                }
                bis.close();
                //删除缓存文件
                if (isDelete) {
                    if (f.isFile() && f.exists()) {
                        f.delete();
                    }
                }

            }
//删除缓存夹
            if (isDelete) {
                File driFile = new File(url);
                if (driFile.exists()) {
                    driFile.delete();
                }
            }
            out.close();
            System.out.println("合并完成");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public static void main(String[] args) {
//        分割文件
//        division("F:\\测试\\开不了口演唱会.mp4",1024*1024*3,null,null,".tmp");
//        合并文件
        merge("F:\\测试\\开不了口演唱会mp4", "1.mp4", null, true);
    }


}

效果图
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荷逸同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值