铂西学习日记---文件分割与合并案例

文件分割与合并案例 :模拟分割大文件功能

比如每次传输有限制,需要将一个大文件分割为多个小文件,然后传输完所有的小文件后合并成一个大文件。

 

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

public class demo_file_dividing_merging {
    //文件分割与合并
    public static void main(String[] args) {
//        File file = new File("D:\\video.mp4");
//        divide(file,1024*1024);//1MB一个视频
        merge();
    }

    /**
     * 文件分割
     * target--目标文件
     * cutSize--每个被分割文件的大小
     */
    public static void divide(File target,long cutSize){
        if (target==null)return;
        //计算总分割的文件数
        int num = target.length()%cutSize == 0 ? (int)(target.length()/cutSize) : (int)(target.length()/cutSize+1);

        try {
            //构造一个文件输入流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(target));
            BufferedOutputStream bos = null;

            byte[] bytes = null;//每次要读的字节数
            int len = -1;//每次实际读取的长度
            int count = 0;//每一个文件要读取的次数

            //处理文件(输出)循环次数为生成文件的个数
            for (int i=0;i<num;i++){
                //构造一个文件输出流,每个文件输出到哪...
                bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\test\\"+(i+1)+"-temp-"+target.getName())));

                //字节处理和读取次数处理----1024可以随便定义建议再大一点
                if(cutSize<=1024){
                    bytes = new byte[(int)cutSize]; //每次读的字节数
                    count = 1; //读一次
                }else {//cutSize>1024
                    bytes = new byte[1024];
                    //可能会被整除,有余数再另算,没余数就这些次数
                    count = (int)cutSize/1024;
                }

                //循环读
                while (count>0 && (len=bis.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                    bos.flush();
                    count--;
                }

                //有余数再读一次
                if(cutSize % 1024 != 0){
                    bytes = new byte[(int) cutSize % 1024];
                    len = bis.read(bytes);
                    bos.write(bytes,0,len);
                    bos.flush();
                }
                //每次读完关闭输出流
                bos.close();
            }

            //最后关闭输入流
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件合并
     * 合并流:SequenceInputStream(Enumeration<? extends InputStream> e)
     */
    public static void merge(){
        try {
            //已被分割的文件
            InputStream in1 = new FileInputStream(new File("D:\\test\\1-temp-video.mp4"));
            InputStream in2 = new FileInputStream(new File("D:\\test\\2-temp-video.mp4"));
            InputStream in3 = new FileInputStream(new File("D:\\test\\3-temp-video.mp4"));

            Vector<InputStream> vector = new Vector<InputStream>();
            vector.add(in1);
            vector.add(in2);
            vector.add(in3);

            Enumeration<InputStream> es = vector.elements();

            //合并流登场
            SequenceInputStream sis = new SequenceInputStream(es);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\test.mp4")));

            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len = sis.read(bytes))!=-1){
                bos.write(bytes,0,len);
                bos.flush();
            }

            //关闭所有的流
            bos.close();
            sis.close();
            System.out.println("合并完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小狗铂西

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

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

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

打赏作者

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

抵扣说明:

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

余额充值