• 文件分割与合并


    合并流SequenceInputStream

    import java.io.*;
    import java.util.Enumeration;
    import java.util.Vector;
    ​
    public class Demo01 {
        /**
         * 文件的分割
         * targetFile 要分割的目标文件
         * cutSize 每个文件的大小
         */
    ​
        private static void division(File targetFile, long cutSize) {
            if(targetFile==null)return;
            //计算一下总共要切割多少次
            int sum = 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;i0 && (len=in.read(bytes ))!=-1){
                        out.write(bytes,0,len);
                        out.flush();
                        count--;
                    }
                    //判断是否还有余数
                    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();
            }
        }
        //合并
        private static void merge(Enumeration  es){
            //构造一个合并流
            SequenceInputStream sis = new SequenceInputStream(es);
            try {
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream("D:\\测试\\西瓜\\1_temp_奇迹·笨小孩 270P(流畅).qlv"));
                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:\\QLDownload\\奇迹·笨小孩 270P(流畅).qlv");
            //division(file,1024*1024*20);
            try {
                InputStream in1 = new FileInputStream(new File("D:\\测试\\1_temp_奇迹·笨小孩 270P(流畅).qlv"));
                InputStream in2 = new FileInputStream(new File("D:\\测试\\2_temp_奇迹·笨小孩 270P(流畅).qlv"));
                InputStream in3 = new FileInputStream(new File("D:\\测试\\3_temp_奇迹·笨小孩 270P(流畅).qlv"));
                InputStream in4 = new FileInputStream(new File("D:\\测试\\4_temp_奇迹·笨小孩 270P(流畅).qlv"));
                InputStream in5 = new FileInputStream(new File("D:\\测试\\5_temp_奇迹·笨小孩 270P(流畅).qlv"));
                InputStream in6 = new FileInputStream(new File("D:\\测试\\6_temp_奇迹·笨小孩 270P(流畅).qlv"));
                InputStream in7 = new FileInputStream(new File("D:\\测试\\7_temp_奇迹·笨小孩 270P(流畅).qlv"));
    ​
                //集合工具类,内部实现使用了数组
                Vector v = new Vector();
                v.add(in1);
                v.add(in2);
                v.add(in3);
                v.add(in4);
                v.add(in5);
                v.add(in6);
                v.add(in7);
                
               Enumeration  es =  v.elements();
               merge(es);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

  • 相关阅读:
    汪子熙趣味成语接龙的游戏软件设计架构说明
    Matlab操作HDF5文件示例
    RabbitMQ中的核心概念和交换机类型
    VUE 组件
    Web 3.0 :它是互联网的未来吗?
    从零开始Blazor Server(9)--修改Layout
    Day34力扣打卡
    设置爱奇艺代理教程
    设计模式之策略模式(场景说明)
    数字IC/FPGA面试题目合集解析(一)
  • 原文地址:https://blog.csdn.net/qq_51058303/article/details/126898277