java文件分片与合并:RandomAccessFile+FileInputStream+FileOutputStream

使用 RandomAccessFile+FileInputStream+FileOutputStream 实现文件的分片与合并
代码贴在文章最后中!

常量解释

  • CHUNK_SIZE:分片后,每片的大小,当前为50MB
  • FILE_PATH:进行分片的文件地址

方法解释

  • test():junit入口,相当于main方法,可改成 public static void main(String[] args),改成main后,其余方法需要加上static修饰
  • split():给文件进行分片的方法,返回分片的片数
  • merge():合并文件

效果展示

运行效果

源代码如下

import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;

/**
 * 文件分片和合并
 *
 *
 */
public class MergeFileTest {

    /**
     * 每片的大小:50MB
     */
    private static final int CHUNK_SIZE = 1024 * 1024 * 50;
    /**
     * 文件地址
     */
    private static final String FILE_PATH = "D:\\files\\test\\video.mp4";

    @Test
    public void test() {
        // junit入口,相当于main方法

        File file = new File(FILE_PATH);
        if (!file.exists()) {
            System.out.println("文件不存在");
        }

        // 分片
        int chunkCount = split(FILE_PATH);

        // 合并
        merge(FILE_PATH, chunkCount);

        System.out.println("合并完成");
    }

    /**
     * 给文件分片
     * @param filePath 文件地址
     * @return 返回片数
     */
    public int split(String filePath) {
        // 片的文件名
        String tmpFileName;
        // 片的数量
        int chunkCount = 0;
        int len;
        byte[] bytes = new byte[CHUNK_SIZE];

        try (FileInputStream fis = new FileInputStream(filePath)) {
            while ((len = fis.read(bytes)) != -1) {
                // 片的文件名:原文件名+片序号
                tmpFileName = filePath + "_" + chunkCount;
                try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFileName)) {
                    fileOutputStream.write(bytes, 0, len);
                }
                chunkCount++;
            }
        }
        catch (Exception e) {
            System.out.println("分片发生错误:" + e.getMessage());
        }

        return chunkCount;
    }

    /**
     * 合并文件
     * @param filePath      文件地址
     * @param chunkCount    片数
     */
    public void merge(String filePath, long chunkCount) {
        int i = filePath.lastIndexOf(".");
        // 合并后的新文件名
        String newFileName = filePath.substring(0, i) + "_new." + filePath.substring(i + 1);

        byte[] bytes = new byte[CHUNK_SIZE];
        long pos = 0;
        int len;
        try (RandomAccessFile raf = new RandomAccessFile(newFileName, "rw");) {
            for (int j = 0; j < chunkCount; j++) {
                try (FileInputStream fis = new FileInputStream(filePath + "_" + j)) {
                    while ((len = fis.read(bytes)) != -1) {
                        raf.seek(pos);
                        raf.write(bytes, 0, len);
                        pos += len;
                    }
                }
            }
        }
        catch (Exception e) {
            System.out.println("合并发生错误:" + e.getMessage());
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值