posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java 文件分割

          Posted on 2009-06-14 23:28 Gavin.lee 閱讀(965) 評論(0)  編輯  收藏 所屬分類: Log && File Operate

          今天一哥們在我電腦上下小說,《七界傳說》 啥玩意我也不知道。我?guī)兔ο碌模峦炅藝標(biāo)牢伊耍咏?0M,這玩意放手機(jī)里根本沒法看,哥們說要分,完了就開始拖啊,分啊。后來我一想,不如用程序分,分的還均勻。呵呵,google一下,找了段程序,稍調(diào)試了一把,測試OK.

          package com.handson.bbs;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;

          /**
           * **********************************************
           * @description 文件分割器,
           *         將一個文件分割成若干小文件,將多個小文件合并到一個大文件
           * 
          @author Gavin.lee
           * @date Jun 14, 2009    3:05:08 PM
           * 
          @version 1.0
           ***********************************************
           
          */

          public class FileDivisionUniter {

              
          public static final String SUFFIX = ".txt"// 分割后的文件名后綴

              
          /**
               * 分割文件
               * 
          @param fileName 待分割的文件名
               * 
          @param size 小文件的大小,以字節(jié)為單位
               * 
          @return 分割后小文件的文件名
               * 
          @throws Exception 分割過程中可能拋出的異常
               
          */

              
          public static String[] divide(String fileName, long size) throws Exception {
                  File inFile 
          = new File(fileName);
                  
          if (!inFile.exists() || inFile.isDirectory()) {
                      
          throw new Exception("指定文件不存在!");
                  }

                  
          // 獲得被分割文件父文件,將來被分割成的小文件就存放在這個目錄下
                  File parentFile = inFile.getParentFile();

                  
          // 取得文件的大小
                  long fileLength = inFile.length();
                  
          if (size <= 0)
                      size 
          = fileLength / 2;

                  
          // 取得分割后的小文件的數(shù)目
                  int num = (int) ((fileLength + size - 1/ size);

                  
          // 存放分割后的小文件名
                  String[] outFileNames = new String[num];

                  FileInputStream in 
          = new FileInputStream(inFile);

                  
          // 讀輸入文件流的開始和結(jié)束下標(biāo)
                  long inEndIndex = 0;
                  
          int inBeginIndex = 0;

                  
          // 根據(jù)要分割的數(shù)目分割文件
                  for (int outFileIndex = 0; outFileIndex < num; outFileIndex++{
                      
          // 對于前outFileIndex-1個文件,大小都是size
                      File outFile = new File(parentFile, inFile.getName() + outFileIndex    + SUFFIX);
                      FileOutputStream out 
          = new FileOutputStream(outFile);
                      inEndIndex 
          += size;
                      inEndIndex 
          = (inEndIndex > fileLength) ? fileLength : inEndIndex;
                      
          // 從輸入流中讀取字節(jié)存儲到輸出流中
                      for (; inBeginIndex < inEndIndex; inBeginIndex++)
                          out.write(in.read());
                      out.close();
                      outFileNames[outFileIndex] 
          = outFile.getAbsolutePath();
                  }

                  in.close();
                  
          return outFileNames;
              }


              
          /**
               * 合并文件
               * 
          @param fileNames 帶合并的文件名,是一個數(shù)組
               * 
          @param targerFileName 目標(biāo)文件名
               * 
          @return 目標(biāo)文件的全路徑
               * 
          @throws Exception 合并過程中可能拋出的異常
               
          */

              
          public static String unite(String[] fileNames, String targerFileName)
                      
          throws Exception {
                  File inFile 
          = null;

                  File outFile 
          = new File(targerFileName);
                  FileOutputStream out 
          = new FileOutputStream(outFile);

                  
          for (int i = 0; i < fileNames.length; i++{
                      inFile 
          = new File(fileNames[i]);
                      FileInputStream in 
          = new FileInputStream(inFile);
                      
          int c;
                      
          while ((c = in.read()) != -1)
                          out.write(c);
                      in.close();
                  }

                  out.close();
                  
          return outFile.getAbsolutePath();
              }


              
          public static void main(String[] args) throws Exception {
                  String fileName 
          = "c:/七界傳說.txt";
                  
          long size = 1024*1000;
                  String[] fileNames 
          = FileDivisionUniter.divide(fileName, size);
                  System.out.println(
          "分割文件" + fileName + "結(jié)果");
                  
          for (int i = 0; i < fileNames.length; i++)
                      System.out.println(fileNames[i]);

          //        String newFileName = "c:/newtest.txt";
          //        String arr[] = new String[10];
          //        arr[0] = "c:/七界傳說.txt0.txt";
          //        arr[1] = "c:/七界傳說.txt1.txt";
          //        arr[2] = "c:/七界傳說.txt2.txt";
          //        arr[3] = "c:/七界傳說.txt3.txt";
          //        arr[4] = "c:/七界傳說.txt4.txt";
          //        arr[5] = "c:/七界傳說.txt5.txt";
          //        arr[6] = "c:/七界傳說.txt6.txt";
          //        arr[7] = "c:/七界傳說.txt7.txt";
          //        arr[8] = "c:/七界傳說.txt8.txt";
          //        arr[9] = "c:/七界傳說.txt9.txt";
          //        
          //        System.out.println("合并結(jié)果:"+ FileDivisionUniter.unite(arr, newFileName));
              }


          }

          主站蜘蛛池模板: 施甸县| 麟游县| 固始县| 连平县| 吉安县| 牙克石市| 历史| 平阳县| 四川省| 富蕴县| 江西省| 太湖县| 博罗县| 黎川县| 新乐市| 如皋市| 宁强县| 乌什县| 临泽县| 泽库县| 大渡口区| 错那县| 潢川县| 平谷区| 平邑县| 中阳县| 岑巩县| 星子县| 佛坪县| 涿州市| 夏津县| 都匀市| 台北县| 抚顺县| 师宗县| 高尔夫| 页游| 迁西县| 读书| 辽阳市| 临安市|