栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现大文件的拆分与合并

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现大文件的拆分与合并

文章目录
  • 前言
  • 一、废话少说,直接上代码
  • 二.测试用例
  • 总结


前言

我需要把一个大文件上传到网盘上,由于网盘禁止上传大于4G的文件,所以我想通过Java程序来把大文件分割为小文件后,再上传,等需要的时候,通过程序把文件进行合并即可。

一、废话少说,直接上代码

代码如下:

package com.xm.file;

import java.io.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


public class FileSplitAndCombine {

    private static int THOUSAND_TWENTY_FOUR= 1024; // one thousand and twenty-four



    
    public static void splitFile(String sourcePath, String desPath, String newName) {

        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(sourcePath)))) {
            for (int i = 0; ; i++) {
                String filePath = desPath + File.separatorChar +newName+"_" +i;
                if (singleSplitFile(bis, filePath, THOUSAND_TWENTY_FOUR)) break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    
    private static boolean singleSplitFile(BufferedInputStream bis, String filePath,int partitionSize) throws IOException {
        int length = partitionSize * THOUSAND_TWENTY_FOUR * THOUSAND_TWENTY_FOUR;
        byte[] buffer = new byte[THOUSAND_TWENTY_FOUR];
        int tempLength;

        System.out.println(nowTime() + "----->>>> begin split " + filePath);
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)))) {
            while ((tempLength = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, tempLength);
                if ((length = length - tempLength) <= 0) break;
            }
        }
        System.out.println(nowTime() + "<<<<----- yet finish split" + filePath);

        return tempLength < 0;
    }

    
    public static void combineFile(String sourcePath, String desPath) {

        List sortedFileNames = sortFilesName(desPath);
        if (sortedFileNames == null) return;

        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(sourcePath)))) {
            for (String sortedFileName : sortedFileNames) {
                copyFile(bos, desPath + File.separatorChar + sortedFileName);
            }
            System.out.println("all finish...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    
    private static void copyFile(BufferedOutputStream bos, String des) throws IOException {

        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(des)))) {
            System.out.println(nowTime()+ "------>>>>"+des + "开始合并");
            byte[] bytes = new byte[1024];
            int length;
            while ((length = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, length);
            }
            System.out.println(nowTime()+ "<<<<------"+des + "合并完成");
        }
    }

    
    private static List sortFilesName(String fileDir) {
        File file = new File(fileDir);
        File[] files = file.listFiles();
        if (null == files || files.length == 0) {
            return null;
        }
        return Arrays.stream(files).filter(File::isFile).map(File::getName)
                .sorted(String::compareTo).collect(Collectors.toList());
    }

    private static String nowTime(){
       return new Date().toLocaleString();
    }
}

二.测试用例

代码如下:

package com.xm;

import com.xm.file.FileSplitAndCombine;


public class FileSplitAndCombineTest {
    public static void main(String[] args) {
       //文件分割
        splitTest();
        //文件合并
        combineTest();
    }

    private static void splitTest() {
        String sourcePath = "E:\fileSplitTest\source.zip";
        String desPath = "E:\fileSplitTest";
        String newName = "split";

        FileSplitAndCombine.splitFile(sourcePath, desPath, newName);
    }

    private static void combineTest() {
        String sourcePath = "E:\fileSplitTest\combine.zip";
        String desPath = "E:\fileSplitTest";

        FileSplitAndCombine.combineFile(sourcePath, desPath);
    }
}


总结

代码比较简单,希望能够帮助到有需要的人,如果您有任何疑问都可以通过评论联系我。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962198.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号