文件的分割与合并,文件传输,

本文介绍了一个Java程序,它实现了文件的分割功能,将大文件如'飞驰人生.mp4'按每10MB大小切割成多个小文件,并展示了如何合并这些小文件回原文件。操作适用于处理大型媒体文件的高效管理和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class test2 {
    //文件的分割
    //targetFile要分割的目标文件
    //cutSize每个文件大小
    private static void division(File targetFile,long cutSize){
        if (targetFile == null)return;
        //计算总分割的文件数
        int num=targetFile.length()%cutSize==0?(int)(targetFile.length()/cutSize):(int)(targetFile.length()/cutSize+1);
        //构造一个文件输入流
        try {
            BufferedInputStream in=new BufferedInputStream(new FileInputStream(targetFile));
            BufferedOutputStream out= null;
            byte[] bytes=null;//每次要读取的字节数
            int len=-1;//每次实际读取的长度
            int count=0;//每一个文件要读取的次数
            for (int i=0;i<num;i++){
                out=new BufferedOutputStream(
                        new FileOutputStream(new File(
                                "D:\\test2\\"+(i+1)+"-temp-"+targetFile.getName())));
                if (cutSize<=1024){
                    bytes=new byte[(int) cutSize];
                    count=1;
                }else {
                    bytes=new byte[1024];
                    count= (int) (cutSize/1024);
                }
                while (count>0 && (len=in.read(bytes))!=-1){
                    out.write(bytes,0,len);
                    out.flush();
                    count--;
                }
                //计算每个文件大小除以1024的余数,决定是否要再读取一次
                if (cutSize%1024 != 0){
                    bytes=new byte[(int) (cutSize%1024)];
                    len=in.read(bytes);
                    out.write(bytes,0,len);
                    out.flush();
                    out.close();
                }
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("文件分割完成...");
        }
    }
    //文件合并
    private static void merge(Enumeration<InputStream> es){
        SequenceInputStream sis =new SequenceInputStream(es);
        try {
            BufferedOutputStream bos=new BufferedOutputStream(
                    new FileOutputStream("d:\\test2\\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 (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
//        File file = new File("d:\\test\\飞驰人生.mp4");
//        division(file,1024*1024*100);
        try {
            InputStream in1=new FileInputStream(new File("d:\\test2\\1-temp-飞驰人生.mp4"));
            InputStream in2=new FileInputStream(new File("d:\\test2\\2-temp-飞驰人生.mp4"));
            InputStream in3=new FileInputStream(new File("d:\\test2\\3-temp-飞驰人生.mp4"));
            InputStream in4=new FileInputStream(new File("d:\\test2\\4-temp-飞驰人生.mp4"));
            InputStream in5=new FileInputStream(new File("d:\\test2\\5-temp-飞驰人生.mp4"));
            InputStream in6=new FileInputStream(new File("d:\\test2\\6-temp-飞驰人生.mp4"));
            Vector<InputStream> v=new Vector<InputStream>();
            v.add(in1);
            v.add(in2);
            v.add(in3);
            v.add(in4);
            v.add(in5);
            v.add(in6);
            Enumeration<InputStream> es=v.elements();
            merge(es);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值