文件太大了,怎么办,吃了它?分了它?我帮您想想办法。

文件太大了,怎么办?

         在开发进程中,众人往往会因文件传输问题而倍感困扰。文件体积过大,占用带宽尚属小事,更棘手的是传输中途突然中断,而后又得重新开始。不过,断点续传算是解决此难题的途径之一。此外,还有一法,即将一个大型文件拆分为多个种子,待全部传送完毕,再进行组装。下面,我以 Java 语言为例加以说明。

1、拆分文件代码

import java.io.*;

public class FileSplitter {
    public static void main(String[] args) {
        String sourceFilePath = "E:other/数字化转型建设方案.zip"; 
        // 分割文件的大小,这里设为10MB,可以根据你的情况修改。
        long chunkSize = 10 * 1024 * 1024; 
        splitFile(sourceFilePath, chunkSize);
    }

    private static void splitFile(String sourceFilePath, long chunkSize) {
        File sourceFile = new File(sourceFilePath);
        if (!sourceFile.exists() || !sourceFile.isFile()) {
            return;
        }

        try (BufferedInputStream bis = new BufferedInputStream(new         rceFile.getName().substring(sourceFile.getName().lastIndexOf('.') + 1);

            while ((bytesRead = bis.read(buffer)) != -1) {
                File partFile = new File(sourceFile.getParent(), fileNameWithoutExt +         
                        bos.write(buffer, 0, bytesRead);
                        bytesWritten += bytesRead;
                        if (chunkSize != -1 && bytesWritten >= chunkSize) break;
                        bytesRead = bis.read(buffer);
                    }
                }
                index++;
            }
            System.out.println("文件分割完成,共生成了" + index + "个文件");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、组装文件代码

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FileAssembler {
    public static void main(String[] args) {
        // 分割后文件所在的目录路径
        String sourceDirectoryName = "E:other/file";
        // 目标文件路径,注意,这里是文件名
        String targetFile = "d:/file/text.rar";
        assembleFiles(sourceDirectoryName, targetFile);
    }

    private static void assembleFiles(String sourceDirPath, String targetFile) {
        Path directoryPath = Paths.get(sourceDirPath);
        if (!Files.isDirectory(directoryPath)) {
            return;
        }

        try (BufferedOutputStream bos = new                 BufferedOutputStream(Files.newOutputStream(Paths.get(targetFile)))) {
            // 获取所有部分文件,请注意,所有名称顺序要按生成的命名方式,不能修改
            Stream<Path> paths = Files.list(directoryPath)
                    .filter(Files::isRegularFile)
                    .sorted();
            for (Path partFile : paths.collect(Collectors.toList())) {
                try (BufferedInputStream bis = new BufferedInputStream(new                     FileInputStream(partFile.toFile()))) {
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = bis.read(buffer)) != -1) {
                        bos.write(buffer, 0, bytesRead);
                    }
                }
            }
            System.out.println("文件组装已完成。请到目标路径下查看。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

英雄汉孑

每一滴心意都是对知识分享的赞美

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

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

打赏作者

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

抵扣说明:

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

余额充值