java分割大文件和合并小文件实例(转)

  1. import java.io.BufferedReader;      
  2. import java.io.BufferedWriter;      
  3. import java.io.File;      
  4. import java.io.FileInputStream;      
  5. import java.io.FileOutputStream;      
  6. import java.io.IOException;      
  7. import java.io.InputStream;      
  8. import java.io.InputStreamReader;      
  9. import java.io.OutputStream;      
  10. import java.io.OutputStreamWriter;      
  11.      
  12. /**    
  13.  * 对文件进行分割与合并的工具    
  14.  *     
  15.  * @author Administrator    
  16.  */     
  17. public class CutJoinFile {      
  18.      
  19.   private static int MAX_PART = 100// 最大分割100分,以免手误引起麻烦      
  20.      
  21.   public static void main(String[] args) throws IOException {      
  22.     String filname = "d:/cvs.rar";      
  23.     cutFileBin(filname, 2 * 1024 * 1024); // 二进制文件为例      
  24.     joinFileBin(filname); // 二进制文件为例      
  25.   }      
  26.      
  27.   /**    
  28.    * 分割文本文件    
  29.    *     
  30.    * @param fileName 文件名    
  31.    * @param size 每个分割小文件的尺寸    
  32.    * @throws IOException    
  33.    */     
  34.   public static void cutFileText(String fileName, int size) throws IOException {      
  35.     BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));      
  36.     int index = fileName.lastIndexOf(".");      
  37.     String ext = "";      
  38.     if (index > 0) {      
  39.       ext = fileName.substring(index);      
  40.       fileName = fileName.substring(0, index);      
  41.     }      
  42.     int len;      
  43.     int count = 1;      
  44.     char[] cbuf = new char[size];      
  45.     while ((len = reader.read(cbuf, 0, size)) != -1) {      
  46.       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + "_" + count + ext)));      
  47.       writer.write(cbuf, 0, len);      
  48.       writer.flush();      
  49.       writer.close();      
  50.       count++;      
  51.       if (count > MAX_PART) {      
  52.         break;      
  53.       }      
  54.     }      
  55.   }      
  56.      
  57.   /**    
  58.    * 分割二进制文件    
  59.    *     
  60.    * @param fileName 文件名    
  61.    * @param size 每个分割小文件的尺寸    
  62.    * @throws IOException    
  63.    */     
  64.   public static void cutFileBin(String fileName, int size) throws IOException {      
  65.     InputStream is = new FileInputStream(fileName);      
  66.     int index = fileName.lastIndexOf(".");      
  67.     String ext = "";      
  68.     if (index > 0) {      
  69.       ext = fileName.substring(index);      
  70.       fileName = fileName.substring(0, index);      
  71.     }      
  72.     int len;      
  73.     int count = 1;      
  74.     byte[] cbuf = new byte[size];      
  75.     while ((len = is.read(cbuf, 0, size)) != -1) {      
  76.       OutputStream os = new FileOutputStream(fileName + "_" + count + ext);      
  77.       os.write(cbuf, 0, len);      
  78.       os.close();      
  79.       count++;      
  80.       if (count > MAX_PART) {      
  81.         break;      
  82.       }      
  83.     }      
  84.   }      
  85.      
  86.   /**    
  87.    * 合并文本文件    
  88.    *     
  89.    * @param fileName 文件名,不包括编号部分    
  90.    * @throws IOException    
  91.    */     
  92.   public static void joinFileText(String fileName) throws IOException {      
  93.     int index = fileName.lastIndexOf(".");      
  94.     String ext = "";      
  95.     if (index > 0) {      
  96.       ext = fileName.substring(index);      
  97.       fileName = fileName.substring(0, index);      
  98.     }      
  99.      
  100.     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + "_join" + ext)));      
  101.     int len;      
  102.     char[] cbuf = new char[1024 * 100];      
  103.     File file;      
  104.     for (int i = 1; i <= Integer.MAX_VALUE; i++) {      
  105.       file = new File(fileName + "_" + i + ext);      
  106.       if (!file.exists()) {      
  107.         break;      
  108.       }      
  109.       BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));      
  110.       while ((len = reader.read(cbuf, 0, cbuf.length)) != -1) {      
  111.         writer.write(cbuf, 0, len);      
  112.         writer.flush();      
  113.       }      
  114.       reader.close();      
  115.     }      
  116.     writer.close();      
  117.   }      
  118.      
  119.   /**    
  120.    * 合并二进制文件    
  121.    *     
  122.    * @param fileName 文件名,不包括编号部分    
  123.    * @throws IOException    
  124.    */     
  125.   public static void joinFileBin(String fileName) throws IOException {      
  126.     int index = fileName.lastIndexOf(".");      
  127.     String ext = "";      
  128.     if (index > 0) {      
  129.       ext = fileName.substring(index);      
  130.       fileName = fileName.substring(0, index);      
  131.     }      
  132.     OutputStream writer = new FileOutputStream(fileName + "_join" + ext);      
  133.     int len;      
  134.     byte[] cbuf = new byte[1024 * 100];      
  135.     File file;      
  136.     for (int i = 1; i <= Integer.MAX_VALUE; i++) {      
  137.       file = new File(fileName + "_" + i + ext);      
  138.       if (!file.exists()) {      
  139.         break;      
  140.       }      
  141.       InputStream reader = new FileInputStream(file);      
  142.       while ((len = reader.read(cbuf, 0, cbuf.length)) != -1) {      
  143.         writer.write(cbuf, 0, len);      
  144.         writer.flush();      
  145.       }      
  146.       reader.close();      
  147.     }      
  148.     writer.close();      
  149.   }      
  150. }    
import java.io.BufferedReader;   
import java.io.BufferedWriter;   
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.InputStreamReader;   
import java.io.OutputStream;   
import java.io.OutputStreamWriter;   
  
/**  
 * 对文件进行分割与合并的工具  
 *   
 * @author Administrator  
 */  
public class CutJoinFile {   
  
  private static int MAX_PART = 100; // 最大分割100分,以免手误引起麻烦   
  
  public static void main(String[] args) throws IOException {   
    String filname = "d:/cvs.rar";   
    cutFileBin(filname, 2 * 1024 * 1024); // 二进制文件为例   
    joinFileBin(filname); // 二进制文件为例   
  }   
  
  /**  
   * 分割文本文件  
   *   
   * @param fileName 文件名  
   * @param size 每个分割小文件的尺寸  
   * @throws IOException  
   */  
  public static void cutFileText(String fileName, int size) throws IOException {   
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));   
    int index = fileName.lastIndexOf(".");   
    String ext = "";   
    if (index > 0) {   
      ext = fileName.substring(index);   
      fileName = fileName.substring(0, index);   
    }   
    int len;   
    int count = 1;   
    char[] cbuf = new char[size];   
    while ((len = reader.read(cbuf, 0, size)) != -1) {   
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + "_" + count + ext)));   
      writer.write(cbuf, 0, len);   
      writer.flush();   
      writer.close();   
      count++;   
      if (count > MAX_PART) {   
        break;   
      }   
    }   
  }   
  
  /**  
   * 分割二进制文件  
   *   
   * @param fileName 文件名  
   * @param size 每个分割小文件的尺寸  
   * @throws IOException  
   */  
  public static void cutFileBin(String fileName, int size) throws IOException {   
    InputStream is = new FileInputStream(fileName);   
    int index = fileName.lastIndexOf(".");   
    String ext = "";   
    if (index > 0) {   
      ext = fileName.substring(index);   
      fileName = fileName.substring(0, index);   
    }   
    int len;   
    int count = 1;   
    byte[] cbuf = new byte[size];   
    while ((len = is.read(cbuf, 0, size)) != -1) {   
      OutputStream os = new FileOutputStream(fileName + "_" + count + ext);   
      os.write(cbuf, 0, len);   
      os.close();   
      count++;   
      if (count > MAX_PART) {   
        break;   
      }   
    }   
  }   
  
  /**  
   * 合并文本文件  
   *   
   * @param fileName 文件名,不包括编号部分  
   * @throws IOException  
   */  
  public static void joinFileText(String fileName) throws IOException {   
    int index = fileName.lastIndexOf(".");   
    String ext = "";   
    if (index > 0) {   
      ext = fileName.substring(index);   
      fileName = fileName.substring(0, index);   
    }   
  
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName + "_join" + ext)));   
    int len;   
    char[] cbuf = new char[1024 * 100];   
    File file;   
    for (int i = 1; i <= Integer.MAX_VALUE; i++) {   
      file = new File(fileName + "_" + i + ext);   
      if (!file.exists()) {   
        break;   
      }   
      BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));   
      while ((len = reader.read(cbuf, 0, cbuf.length)) != -1) {   
        writer.write(cbuf, 0, len);   
        writer.flush();   
      }   
      reader.close();   
    }   
    writer.close();   
  }   
  
  /**  
   * 合并二进制文件  
   *   
   * @param fileName 文件名,不包括编号部分  
   * @throws IOException  
   */  
  public static void joinFileBin(String fileName) throws IOException {   
    int index = fileName.lastIndexOf(".");   
    String ext = "";   
    if (index > 0) {   
      ext = fileName.substring(index);   
      fileName = fileName.substring(0, index);   
    }   
    OutputStream writer = new FileOutputStream(fileName + "_join" + ext);   
    int len;   
    byte[] cbuf = new byte[1024 * 100];   
    File file;   
    for (int i = 1; i <= Integer.MAX_VALUE; i++) {   
      file = new File(fileName + "_" + i + ext);   
      if (!file.exists()) {   
        break;   
      }   
      InputStream reader = new FileInputStream(file);   
      while ((len = reader.read(cbuf, 0, cbuf.length)) != -1) {   
        writer.write(cbuf, 0, len);   
        writer.flush();   
      }   
      reader.close();   
    }   
    writer.close();   
  }   
}  

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值