Java-使用IO流对大文件进行分割和分割后的合并

有的时候我们想要操作的文件很大,比如:我们想要上传一个大文件,但是受到上传文件大小的限制,无法上传,这时我们可以将一个大的文件分割成若干个小文件进行操作,然后再把小文件还原成源文件。分割后的每个小文件的类型可以自己定义。

以下是我编写的一个大文件的分割和合并的代码:

[java]  view plain  copy
  1. package com.lym;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  11. import java.io.OutputStream;  
  12.   
  13. /** 
  14.  * 1、实现一个文件分割器,把一个大文件分割成若干个小文件(可根据情况自行设计), 
  15.  * 分割后的文件扩展名为dat,文件名为:原文件名+原扩展名+编号+.dat 
  16.  * 2、把分割后的文件再合并(文件还原)成完整文件,与源文件一致。 
  17.  * @author lym 
  18.  * 
  19.  */  
  20. public class Homework2 {  
  21.   
  22.     /** 
  23.      * 文件分割 
  24.      * @param src 源文件路径 
  25.      * @param fileSize 分割后每个文件的大小,单位是MB 
  26.      * @param dest 目标文件路径 
  27.      */  
  28.     public static void split(String src,int fileSize,String dest){  
  29.           
  30.         if("".equals(src)||src==null||fileSize==0||"".equals(dest)||dest==null){  
  31.             System.out.println("分割失败");  
  32.         }  
  33.           
  34.         File srcFile = new File(src);//源文件  
  35.           
  36.         long srcSize = srcFile.length();//源文件的大小  
  37.         long destSize = 1024*1024*fileSize;//目标文件的大小(分割后每个文件的大小)  
  38.           
  39.         int number = (int)(srcSize/destSize);  
  40.         number = srcSize%destSize==0?number:number+1;//分割后文件的数目  
  41.           
  42.         String fileName = src.substring(src.lastIndexOf("\\"));//源文件名  
  43.           
  44.         InputStream in = null;//输入字节流  
  45.         BufferedInputStream bis = null;//输入缓冲流  
  46.         byte[] bytes = new byte[1024*1024];//每次读取文件的大小为1MB  
  47.         int len = -1;//每次读取的长度值  
  48.         try {  
  49.             in = new FileInputStream(srcFile);  
  50.             bis = new BufferedInputStream(in);  
  51.             for(int i=0;i
  52.                   
  53.                 String destName = dest+File.separator+fileName+"-"+i+".dat";  
  54.                 OutputStream out = new FileOutputStream(destName);  
  55.                 BufferedOutputStream bos = new BufferedOutputStream(out);  
  56.                 int count = 0;  
  57.                 while((len = bis.read(bytes))!=-1){  
  58.                     bos.write(bytes, 0, len);//把字节数据写入目标文件中  
  59.                     count+=len;  
  60.                     if(count>=destSize){  
  61.                         break;  
  62.                     }  
  63.                 }  
  64.                 bos.flush();//刷新  
  65.                 bos.close();  
  66.                 out.close();  
  67.             }  
  68.         } catch (FileNotFoundException e) {  
  69.             e.printStackTrace();  
  70.         } catch (IOException e) {  
  71.             e.printStackTrace();  
  72.         }finally{  
  73.             //关闭流  
  74.             try {  
  75.                 if(bis!=null)bis.close();  
  76.                 if(in!=null)in.close();  
  77.             } catch (IOException e) {  
  78.                 e.printStackTrace();  
  79.             }  
  80.         }  
  81.     }  
  82.       
  83.     /** 
  84.      * 文件合并 
  85.      * 注意:在拼接文件路劲时,一定不要忘记文件的跟路径,否则复制不成功 
  86.      * @param destPath 目标目录 
  87.      * @param srcPaths 源文件目录 
  88.      */  
  89.     public static void merge(String destPath,String ... srcPaths){  
  90.         if(destPath==null||"".equals(destPath)||srcPaths==null){  
  91.             System.out.println("合并失败");  
  92.         }  
  93.         for (String string : srcPaths) {  
  94.             if("".equals(string)||string==null)  
  95.                 System.out.println("合并失败");  
  96.         }  
  97.         //合并后的文件名  
  98.         String name = srcPaths[0].substring(srcPaths[0].lastIndexOf("\\"));  
  99.         String destName = name.substring(0, name.lastIndexOf("-"));  
  100.         destPath = destPath+destName;//合并后的文件路径  
  101.           
  102.         File destFile = new File(destPath);//合并后的文件  
  103.         OutputStream out = null;  
  104.         BufferedOutputStream bos = null;  
  105.         try {  
  106.             out = new FileOutputStream(destFile);  
  107.             bos = new BufferedOutputStream(out);  
  108.             for (String src : srcPaths) {  
  109.                 File srcFile = new File(src);  
  110.                 InputStream in = new FileInputStream(srcFile);  
  111.                 BufferedInputStream bis = new BufferedInputStream(in);  
  112.                 byte[] bytes = new byte[1024*1024];  
  113.                 int len = -1;  
  114.                 while((len = bis.read(bytes))!=-1){  
  115.                     bos.write(bytes, 0, len);  
  116.                 }  
  117.                 bis.close();  
  118.                 in.close();  
  119.             }  
  120.         } catch (FileNotFoundException e) {  
  121.             e.printStackTrace();  
  122.         } catch (IOException e) {  
  123.             e.printStackTrace();  
  124.         }finally{  
  125.             //关闭流  
  126.             try {  
  127.                 if(bos!=null)bos.close();  
  128.                 if(out!=null)out.close();  
  129.             } catch (IOException e) {  
  130.                 e.printStackTrace();  
  131.             }  
  132.         }  
  133.     }  
  134.       
  135.     public static void main(String[] args) {  
  136.         /** 
  137.          * 分割测试 
  138.          */  
  139. //      String src = "E:\\API\\JDK_API_1_6_zh_CN.CHM";//要分割的大文件  
  140. //      int fileSize = 10;  
  141. //      String dest = "D:\\";//文件分割后保存的路径  
  142. //      System.out.println("分割开始。。。");  
  143. //      split(src, fileSize, dest);  
  144. //      System.out.println("分割完成");  
  145.           
  146.         /** 
  147.          * 合并测试 
  148.          */  
  149.         //合并后文件的保存路径  
  150.         String destPath = "D:\\upan";  
  151.         //要合并的文件路径  
  152.         String[] srcPaths = {  
  153.                 "D:\\JDK_API_1_6_zh_CN.CHM-0.dat",  
  154.                 "D:\\JDK_API_1_6_zh_CN.CHM-1.dat",  
  155.                 "D:\\JDK_API_1_6_zh_CN.CHM-2.dat",  
  156.                 "D:\\JDK_API_1_6_zh_CN.CHM-3.dat"};  
  157.         System.out.println("开始合并。。。");  
  158.         merge(destPath, srcPaths);  
  159.         System.out.println("合并结束");  
  160.     }  
  161.   
  162. }  

你可能感兴趣的:(java基础)