java切割合并文件

package com.zhjy.function.storeHouse;

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.OutputStream;  
 
/** 
* 1、实现一个文件分割器,把一个大文件分割成若干个小文件(可根据情况自行设计), 
* 分割后的文件扩展名为dat,文件名为:原文件名+原扩展名+编号+.dat 
* 2、把分割后的文件再合并(文件还原)成完整文件,与源文件一致。 
* @author lym 
* 
*/  
public class Homework2 {  
 
   /** 
    * 文件分割 
    * @param src 源文件路径 
    * @param fileSize 分割后每个文件的大小,单位是MB 
    * @param dest 目标文件路径 
    */  
   public static void split(String src,int fileSize,String dest){  
         
       if("".equals(src)||src==null||fileSize==0||"".equals(dest)||dest==null){  
           System.out.println("分割失败");  
       }  
         
       File srcFile = new File(src);//源文件  
         
       long srcSize = srcFile.length();//源文件的大小  
       long destSize = 1024*1024*fileSize;//目标文件的大小(分割后每个文件的大小)  
         
       int number = (int)(srcSize/destSize);  
       number = srcSize%destSize==0?number:number+1;//分割后文件的数目  
         
       String fileName = src.substring(src.lastIndexOf("\\"));//源文件名  
         
       InputStream in = null;//输入字节流  
       BufferedInputStream bis = null;//输入缓冲流  
       byte[] bytes = new byte[1024*1024];//每次读取文件的大小为1MB  
       int len = -1;//每次读取的长度值  
       try {  
           in = new FileInputStream(srcFile);  
           bis = new BufferedInputStream(in);  
           for(int i=0;i<number;i++){  
                 
               String destName = dest+File.separator+fileName+"-"+i+".sql";  
               OutputStream out = new FileOutputStream(destName);  
               BufferedOutputStream bos = new BufferedOutputStream(out);  
               int count = 0;  
               while((len = bis.read(bytes))!=-1){  
                   bos.write(bytes, 0, len);//把字节数据写入目标文件中  
                   count+=len;  
                   if(count>=destSize){  
                       break;  
                   }  
               }  
               bos.flush();//刷新  
               bos.close();  
               out.close();  
           }  
       } catch (FileNotFoundException e) {  
           e.printStackTrace();  
       } catch (IOException e) {  
           e.printStackTrace();  
       }finally{  
           //关闭流  
           try {  
               if(bis!=null)bis.close();  
               if(in!=null)in.close();  
           } catch (IOException e) {  
               e.printStackTrace();  
           }  
       }  
   }  
     
   /** 
    * 文件合并 
    * 注意:在拼接文件路劲时,一定不要忘记文件的跟路径,否则复制不成功 
    * @param destPath 目标目录 
    * @param srcPaths 源文件目录 
    */  
   public static void merge(String destPath,String ... srcPaths){  
       if(destPath==null||"".equals(destPath)||srcPaths==null){  
           System.out.println("合并失败");  
       }  
       for (String string : srcPaths) {  
           if("".equals(string)||string==null)  
               System.out.println("合并失败");  
       }  
       //合并后的文件名  
       String name = srcPaths[0].substring(srcPaths[0].lastIndexOf("\\"));  
       String destName = name.substring(0, name.lastIndexOf("-"));  
       destPath = destPath+destName;//合并后的文件路径  
         
       File destFile = new File(destPath);//合并后的文件  
       OutputStream out = null;  
       BufferedOutputStream bos = null;  
       try {  
           out = new FileOutputStream(destFile);  
           bos = new BufferedOutputStream(out);  
           for (String src : srcPaths) {  
               File srcFile = new File(src);  
               InputStream in = new FileInputStream(srcFile);  
               BufferedInputStream bis = new BufferedInputStream(in);  
               byte[] bytes = new byte[1024*1024];  
               int len = -1;  
               while((len = bis.read(bytes))!=-1){  
                   bos.write(bytes, 0, len);  
               }  
               bis.close();  
               in.close();  
           }  
       } catch (FileNotFoundException e) {  
           e.printStackTrace();  
       } catch (IOException e) {  
           e.printStackTrace();  
       }finally{  
           //关闭流  
           try {  
               if(bos!=null)bos.close();  
               if(out!=null)out.close();  
           } catch (IOException e) {  
               e.printStackTrace();  
           }  
       }  
   }  
     
   public static void main(String[] args) {  
       /** 
        * 分割测试 
        */  
     //String src = "E:\\API\\JDK_API_1_6_zh_CN.CHM";//要分割的大文件  
     String src = "D:\\用户目录\\我的音乐\\wjwzhbgmh.sql";//要分割的大文件  
     int fileSize = 100;  
     String dest = "F:\\大sql";//文件分割后保存的路径  
     System.out.println("分割开始。。。");  
     split(src, fileSize, dest);  
     System.out.println("分割完成");  
         
       /** 
        * 合并测试 
        */  
//       合并后文件的保存路径  
//       String destPath = "D:\\upan";  
//       要合并的文件路径  
//       String[] srcPaths = {  
//               "D:\\JDK_API_1_6_zh_CN.CHM-0.dat",  
//               "D:\\JDK_API_1_6_zh_CN.CHM-1.dat",  
//               "D:\\JDK_API_1_6_zh_CN.CHM-2.dat",  
//               "D:\\JDK_API_1_6_zh_CN.CHM-3.dat"};  
//       System.out.println("开始合并。。。");  
//       merge(destPath, srcPaths);  
//       System.out.println("合并结束");  
   }  
 
}  
package com.zhjy.function.storeHouse;


import java.io.BufferedReader;
 
import java.io.File;
 
import java.io.FileInputStream;
 
import java.io.FileOutputStream;
 
import java.io.FileReader;
 
import java.io.IOException;
 
  
 
public class pidefile {
 
    public static final String SUFFIX = ".sql"; // 分割后的文件名后缀
 
  
 
    // 将指定的文件按着给定的文件的字节数进行分割文件,其中name指的是需要进行分割的文件名,size指的是指定的小文件的大小
 
    public static String[] pide(String name, long size) throws Exception {
 
        File file = new File(name);
 
        if (!file.exists() || (!file.isFile())) {
 
            throw new Exception("指定文件不存在!");
 
        }
 
        // 获得被分割文件父文件,将来被分割成的小文件便存在这个目录下
 
        File parentFile = file.getParentFile();
 
        // 取得文件的大小
 
        long fileLength = file.length();
 
  
 
        System.out.println("文件大小:" + fileLength + " 字节");
 
  
 
        if (size <= 0) {
 
            size = fileLength / 2;
 
        }
 
        // 取得被分割后的小文件的数目
 
        int num = (fileLength % size != 0) ? (int) (fileLength / size + 1)
 
                : (int) (fileLength / size);
 
        // 存放被分割后的小文件名
 
        String[] fileNames = new String[num];
 
        // 输入文件流,即被分割的文件
 
        FileInputStream in = new FileInputStream(file);
 
        // 读输入文件流的开始和结束下标
 
        long end = 0;
 
        int begin = 0;
 
        // 根据要分割的数目输出文件
 
        for (int i = 0; i < num; i++) {
 
            // 对于前num - 1个小文件,大小都为指定的size
 
            File outFile = new File(parentFile, file.getName() + i + SUFFIX);
 
            // 构建小文件的输出流
 
            FileOutputStream out = new FileOutputStream(outFile);
 
            // 将结束下标后移size
 
            end += size;
 
            end = (end > fileLength) ? fileLength : end;
 
            // 从输入流中读取字节存储到输出流中
 
            for (; begin < end; begin++) {
 
                out.write(in.read());
 
            }
 
            out.close();
 
            fileNames[i] = outFile.getAbsolutePath();
 
        }
 
        in.close();
 
        return fileNames;
 
    }
 
  
 
    public static void readFileMessage(String fileName) {// 将分割成的小文件中的内容读出
 
        File file = new File(fileName);
 
        BufferedReader reader = null;
 
        try {
 
            reader = new BufferedReader(new FileReader(file));
 
            String string = null;
 
            // 按行读取内容,直到读入null则表示读取文件结束
 
            while ((string = reader.readLine()) != null) {
 
                System.out.println(string);
 
            }
 
            reader.close();
 
        } catch (IOException e) {
 
            e.printStackTrace();
 
        } finally {
 
            if (reader != null) {
 
                try {
 
                    reader.close();
 
                } catch (IOException e1) {
 
                }
 
            }
 
        }
 
    }
 
  
 
    public static void main(final String[] args) throws Exception {
 
        //String name = "G:/ad.tar/ad/ad_samples.dat";
        String name = "D:/用户目录/我的音乐/wjwzhbgmh.sql";
        long size = 1024 * 1024 * 4;// 1K=1024b(字节)
 
        String[] fileNames = pidefile.pide(name, size);
 
        System.out.println("文件" + name + "分割的结果如下:");
 
        for (int i = 0; i < fileNames.length; i++) {
 
            System.out.println(fileNames[i] + "的内容如下:");
 
            // FenGeFile.readFileMessage(fileNames[i]);
 
            System.out.println();
 
        }
 
    }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值