文件的分割合并

本文介绍了一种文件分割和合并的技术实现,通过Java代码详细展示了如何将大文件分割成多个小块并存储到指定目录,以及如何将这些小块重新合并为原始文件。此技术在处理大型文件、分布式存储和数据传输场景中尤为实用。

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

package com.bootdo.common.config.collection.test.io;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
 * @Author zhangfu
 * @Description 文件的分割和合并
 * @Date 15:57 2018/11/2
 * @Param
 * @return
 */

public class SplitFile {
    //文件的路径
    private String filePath;
    //文件名
    private String fileName;
    //文件大小
    private long length;
    //块数
    private int size;
    //每块的大小
    private long blockSize;
    //分割后的存放目录
    private String destBlockPath;
    //每块的名称
    private List<String> blockPath;

    public SplitFile(){
        blockPath = new ArrayList<String>();
    }
    public SplitFile(String filePath,String destBlockPath){
        this(filePath,destBlockPath,1024);
    }
    public SplitFile(String filePath,String destBlockPath,long blockSize){
        this();
        this.filePath= filePath;
        this.destBlockPath =destBlockPath;
        this.blockSize=blockSize;
        init();
    }

    /**
     * 初始化操作 计算 块数、确定文件名
     */
    public void init(){
        File src =null;
        //健壮性
        if(null==filePath ||!(((src=new File(filePath)).exists()))){
            return;
        }
        if(src.isDirectory()){
            return ;
        }
        //文件名
        this.fileName =src.getName();

        //计算块数 实际大小 与每块大小
        this.length = src.length();
        //修正 每块大小
        if(this.blockSize>length){
            this.blockSize =length;
        }
        //确定块数
        size= (int)(Math.ceil(length*1.0/this.blockSize));
        //确定文件的路径
        initPathName();
    }

    private void initPathName(){
        for(int i=0;i<size;i++){
            this.blockPath.add(destBlockPath+"/"+this.fileName+"part"+i+this.fileName.substring(this.fileName.lastIndexOf(".")));
        }
    }

    /**
     * 文件的分割
     * 0)、第几块
     * 1、起始位置
     * 2、实际大小
     * @param destPath 分割文件存放目录
     */
    public void split(){
        long beginPos =0;  //起始点
        long actualBlockSize =blockSize; //实际大小
        //计算所有块的大小、位置、索引
        for(int i=0;i<size;i++){
            if(i==size-1){ //最后一块
                actualBlockSize =this.length-beginPos;
            }
            spiltDetail(i,beginPos,actualBlockSize);
            beginPos+=actualBlockSize; //本次的终点,下一次的起点
        }

    }
    /**
     * 文件的分割 输入 输出
     * 文件拷贝
     * @param idx 第几块
     * @param beginPos 起始点
     * @param actualBlockSize 实际大小
     */
    private void spiltDetail(int idx,long beginPos,long actualBlockSize){
        //1、创建源
        File src = new File(this.filePath);  //源文件
        File dest = new File(this.blockPath.get(idx)); //目标文件
        //2、选择流
        RandomAccessFile raf = null;  //输入流
        BufferedOutputStream bos=null; //输出流
        try {
            raf=new RandomAccessFile(src,"r");
            bos =new BufferedOutputStream(new FileOutputStream(dest));

            //读取文件
            raf.seek(beginPos);
            //缓冲区
            byte[] flush = new byte[1024];
            //接收长度
            int len =0;
            while(-1!=(len=raf.read(flush))){
                if(actualBlockSize-len>=0){ //查看是否足够
                    //写出
                    bos.write(flush, 0, len);
                    actualBlockSize-=len; //剩余量
                }else{ //写出最后一次的剩余量
                    bos.write(flush, 0, (int)actualBlockSize);
                    break;
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //FileUtil.close(bos,raf);
        }

    }
    /**
     * 文件的合并
     */
    public void merge(String destPath){
        //创建源
        File dest =new File(destPath);
        //选择流
        BufferedOutputStream bos=null; //输出流
        SequenceInputStream sis =null ;//输入流
        //创建一个容器
        Vector<InputStream> vi = new Vector<InputStream>();
        try {
            for (int i = 0; i < this.blockPath.size(); i++) {
                vi.add(new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i)))));
            }
            bos =new BufferedOutputStream(new FileOutputStream(dest,true)); //追加
            sis=new SequenceInputStream(vi.elements());

            //缓冲区
            byte[] flush = new byte[1024];
            //接收长度
            int len =0;
            while(-1!=(len=sis.read(flush))){
                bos.write(flush, 0, len);
            }
            bos.flush();
           // FileUtil.close(sis);
        } catch (Exception e) {
        }finally{
           // FileUtil.close(bos);
        }

    }
    /**
     * 文件的合并
     */
    public void merge1(String destPath){
        //创建源
        File dest =new File(destPath);
        //选择流
        BufferedOutputStream bos=null; //输出流
        try {
            bos =new BufferedOutputStream(new FileOutputStream(dest,true)); //追加
            BufferedInputStream bis = null;
            for (int i = 0; i < this.blockPath.size(); i++) {
                bis = new BufferedInputStream(new FileInputStream(new File(this.blockPath.get(i))));

                //缓冲区
                byte[] flush = new byte[1024];
                //接收长度
                int len =0;
                while(-1!=(len=bis.read(flush))){
                    bos.write(flush, 0, len);
                }
                bos.flush();
               // FileUtil.close(bis);
            }
        } catch (Exception e) {
        }finally{
           // FileUtil.close(bos);
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SplitFile split = new SplitFile("E:/java高级/课程目录.txt","E:/java高级/分割",10000);
        System.out.println(split.length);
        System.out.println(split.size);

        /// split.split();

        split.merge("E:/java高级/分割/1.txt");
    }



}

 

内容概要:本文档提供了关于“微型车间生产线的设计与生产数据采集试验研究”的毕业设计复现代码,涵盖从论文结构生成、机械结构设计、PLC控制系统设计、生产数据采集与分析系统、有限元分析、进度管理、文献管理和论文排版系统的完整实现。通过Python代码和API调用,详细展示了各个模块的功能实现和相互协作。例如,利用SolidWorks API设计机械结构,通过PLC控制系统模拟生产流程,使用数据分析工具进行生产数据的采集和异常检测,以及利用进度管理系统规划项目时间表。 适合人群:具有机械工程、自动化控制或计算机编程基础的学生或研究人员,尤其是从事智能制造领域相关工作的人员。 使用场景及目标:①帮助学生或研究人员快速搭建和理解微型车间生产线的设计与实现;②提供完整的代码框架,便于修改和扩展以适应不同的应用场景;③作为教学或科研项目的参考资料,用于学习和研究智能制造技术。 阅读建议:此资源不仅包含详细的代码实现,还涉及多个学科领域的知识,如机械设计、电气控制、数据分析等。因此,在学习过程中,建议读者结合实际操作,逐步理解每个模块的功能和原理,并尝试调整参数以观察不同设置下的系统表现。同时,可以参考提供的文献资料,深入研究相关理论和技术背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值