Java实现文件的分割与合并

文件的操作

文件的分割

package com.xhh.util;

import java.io.*;

/**
 * 文件分割的方法
 * @param SrcFilePath 指定分割的文件路径
 * @param SingleGoalFileSize 分割文件的个数
 * @param GoalFileDirectory 分割之后的路径
 */
public class Split{
  public static void Split(String SrcFilePath,int SingleGoalFileSize,String GoalFileDirectory){
    //SingleGoalFileSize 单位:MB   ,校验路径和目录
    if("".equals(SrcFilePath) || SrcFilePath == null || "".equals(GoalFileDirectory) || GoalFileDirectory == null){
        System.out.println("分割失败!");
        return;
    }

    File SrcFile = new File(SrcFilePath);  //新建文件
    long SrcFileSize = SrcFile.length();//源文件的大小
    long SingleFileSize = 1024 * 1024 * SingleGoalFileSize;//分割后的单个文件大小(单位字节)

    int GoalFileNum = (int)(SrcFileSize/SingleFileSize);  //获取文件的大小
    GoalFileNum = SrcFileSize % SingleFileSize == 0 ? GoalFileNum : GoalFileNum + 1;  //计算总的文件大小

    int x1 = SrcFilePath.lastIndexOf("\\"); //获取文件路径的分隔符位置
    int x2 = SrcFilePath.lastIndexOf("."); //获取文件的后缀位置

    String SrcFileName = SrcFilePath.substring(x1,x2); //截取文件名

    FileInputStream fis = null;
    BufferedInputStream bis = null;
    byte bytes[] = new byte[1024 * 1024];//每次读取文件的大小
    int len = -1;

    try{ 
        fis = new FileInputStream(SrcFilePath);  //新建输入流对象
        bis = new BufferedInputStream(fis);  

        for(int i = 0; i < GoalFileNum; i++){
            //分割后的单个文件完整路径名
            String CompleteSingleGoalFilePath = GoalFileDirectory + File.separator + SrcFileName + "-" + i + SrcFilePath.substring(x2);
            FileOutputStream fos = new FileOutputStream(CompleteSingleGoalFilePath);
            BufferedOutputStream bos = new BufferedOutputStream(fos);  //包装
            int count = 0;
            while((len = bis.read(bytes))!=-1){
                bos.write(bytes,0,len);//从源文件读取规定大小的字节数写入到单个目标文件中
                count += len;
                if(count >= SingleFileSize)
                    break;
            }
            bos.flush();
            bos.close();
            fos.close();
        }
         System.out.println("分割成功!");
    }catch (FileNotFoundException e){
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try{
            if(bis != null) {
                bis.close();
            }

            if(fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

运行之后:
在这里插入图片描述
原文件
在这里插入图片描述
分割出来的文件
在这里插入图片描述
文件已经成功的分割出来了

文件的合并

package com.xhh.util;

import java.io.*;

public class Merge {	
/**
 * @param SingleFilePath 
 * @param GoalFileDirectory 
 */
	public static void Merge(String SingleFilePath[],String GoalFileDirectory){
        if(GoalFileDirectory == null || "".equals(GoalFileDirectory)){
            System.out.println("合并失败!");
            return;
        }

        int x1 = SingleFilePath[0].lastIndexOf("\\");
        int x2 = SingleFilePath[0].lastIndexOf(".");
        String GoalFileName = SingleFilePath[0].substring(x1,x2);

        //合并后的完整路径名
        String CompleteGoalFilePath = GoalFileDirectory + File.separator + GoalFileName.substring(0,GoalFileName.lastIndexOf("-"))+ SingleFilePath[0].substring(x2);

        byte bytes[] = new byte[1024 * 1024];//每次读取文件的大小
        int len = -1;

        FileOutputStream fos = null;//将数据合并到目标文件中
        BufferedOutputStream bos = null;//使用缓冲字节流写入数据
        try{
             fos = new FileOutputStream(CompleteGoalFilePath);
             bos = new BufferedOutputStream(fos);

            for(int i = 0; i < SingleFilePath.length; i++){
               if(SingleFilePath[i] == null || "".equals(SingleFilePath)){
                   System.exit(0);
               }

               FileInputStream fis = new FileInputStream(SingleFilePath[i]);//从分割后的文件读取数据
               BufferedInputStream bis = new BufferedInputStream(fis);//使用缓冲字节流读取数据
               while ((len = bis.read(bytes))!= -1)
                   bos.write(bytes, 0, len);

               bis.close();
               fis.close();
           }
            System.out.println("合并成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bos != null)
                    bos.close();

                if(fos != null)
                    fos.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

运行之后:
在这里插入图片描述
原文件
在这里插入图片描述
合并出来的文件
在这里插入图片描述
这样文件已经合并起来了,注意看路径是不一样的

测试

/**
 * 测试
 * @param args
 */
public static void main(String[] args) {
//			Split("D:\\qycache\\download\\dpx\\dpx3.qvs", 10, "D:\\qycache\\download\\hhhh");//
//	        Split(SrcFilePath, SingleGoalFileSize, GoalFileDirectory);
//          SrcFilePath 指定分割的文件路径   SingleGoalFileSize 分割文件的个数   GoalFileDirectory  分割完成之后的路径	        
			String[] MergeFilePath = new String[5];//分割出来的文件个数
	        for(int i = 0; i < MergeFilePath.length; i++)
	            MergeFilePath[i] = new String("D:\\qycache\\download\\hhhh\\dpx3-" + i + ".qsv");//想要合并的文件路径
	            Merge(MergeFilePath,"D:\\qycache\\download\\jjjj");//合并之后保存的路径
		}

注意:
分割过的文件有时会出现文件损坏或丢失的情况,这时就必须由接收将这些文件合并才能回复原来的文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值