無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java 文件分割

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

          今天一哥們在我電腦上下小說,《七界傳說》 啥玩意我也不知道。我幫忙下的,下完了嚇死我了,接近10M,這玩意放手機里根本沒法看,哥們說要分,完了就開始拖啊,分啊。后來我一想,不如用程序分,分的還均勻。呵呵,google一下,找了段程序,稍調試了一把,測試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 小文件的大小,以字節為單位
               * 
          @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;

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

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

                  FileInputStream in 
          = new FileInputStream(inFile);

                  
          // 讀輸入文件流的開始和結束下標
                  long inEndIndex = 0;
                  
          int inBeginIndex = 0;

                  
          // 根據要分割的數目分割文件
                  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;
                      
          // 從輸入流中讀取字節存儲到輸出流中
                      for (; inBeginIndex < inEndIndex; inBeginIndex++)
                          out.write(in.read());
                      out.close();
                      outFileNames[outFileIndex] 
          = outFile.getAbsolutePath();
                  }

                  in.close();
                  
          return outFileNames;
              }


              
          /**
               * 合并文件
               * 
          @param fileNames 帶合并的文件名,是一個數組
               * 
          @param targerFileName 目標文件名
               * 
          @return 目標文件的全路徑
               * 
          @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 + "結果");
                  
          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("合并結果:"+ FileDivisionUniter.unite(arr, newFileName));
              }


          }

          主站蜘蛛池模板: 靖边县| 普定县| 济阳县| 紫金县| 威远县| 连云港市| 望谟县| 齐河县| 西畴县| 清徐县| 封丘县| 黄陵县| 南和县| 成武县| 达孜县| 东源县| 临清市| 新余市| 锡林郭勒盟| 临沧市| 东兰县| 东源县| 龙井市| 琼中| 河源市| 武义县| 垣曲县| 犍为县| 五常市| 兴宁市| 沽源县| 化州市| 垣曲县| 惠安县| 丁青县| 江城| 鞍山市| 丹江口市| 武清区| 新巴尔虎左旗| 桂东县|