對大檔案實現分割及合并處理,實現分割合并

來源:互聯網
上載者:User

對大檔案實現分割及合并處理,實現分割合并

 1   public void cutFile(File bigFile,File destFile,int cutSize){ 2  3         FileInputStream inputStream = null; 4         int size = 1024*1024; //1M 5         try { 6             if (!destFile.isDirectory()){ //如果儲存分割檔案的地址不是路徑 7                 destFile.mkdir(); //建立路徑 8             } 9             size = size * cutSize; //分割檔案大小以M為單位10             int length = (int) bigFile.length(); //擷取大檔案大小(B為單位)11             int num = length / size; //計算分割成小檔案的個數(每個小檔案大小是M為單位)12             int yu = length % size; //除餘的檔案大小(M)13 14             String bigFilePath = bigFile.getAbsolutePath(); //擷取大檔案完整路徑資訊(包括檔案名稱)15             int fileNew = bigFilePath.lastIndexOf("."); //擷取檔案尾碼前的“."的索引16             String suffix = bigFilePath.substring(fileNew,bigFilePath.length()); //擷取尾碼,即檔案類型17 18             inputStream = new FileInputStream(bigFile); //擷取大檔案的檔案輸入資料流19             File[] smallFile = new File[num+1]; //建立儲存小檔案的檔案數組20             int begin = 0;21             for (int i =0;i< num;i++){22                 smallFile[i] = new File(bigFile.getAbsolutePath()+"\\"+(i+1)+suffix+".tem"); //指定小檔案的名字23                 if (!smallFile[i].isFile()){24                     smallFile[i].createNewFile(); //建立該檔案25                 }26                 FileOutputStream outputStream = new FileOutputStream(smallFile[i]); //建立小檔案的檔案輸出資料流27                 byte[] small = new byte[size];28                 inputStream.read(small); //讀取小檔案位元組29                 outputStream.write(small); //向小檔案中寫入位元組資料30                 begin = begin + size;31                 outputStream.close();32             }33             if (yu != 0){ ///除餘的檔案大小(M)部不為空白34                 smallFile[num] = new File(bigFile.getAbsolutePath()+"\\"+(num+1)+suffix+".tem");35                 if (!smallFile[num].isFile()){36                     smallFile[num].createNewFile(); //建立檔案37                 }38                 FileOutputStream outputStream = new FileOutputStream(smallFile[num]);39                 byte[] bytes = new byte[yu];40                 inputStream.read(bytes); //讀取位元組41                 outputStream.write(bytes); //向檔案寫入資料42                 outputStream.close();43             }44         }catch (Exception e){45             e.printStackTrace();46         }47     }

2.合并處理

 1   public void closeFile(File[] files,File closeDir,String hz){ 2         try { 3             File closeFile = new File(closeDir.getAbsoluteFile()+"\\close"+hz); //指定合并後的檔案名稱(包含路徑) 4             if (!closeFile.isFile()){ 5                 closeFile.createNewFile(); //建立檔案 6             } 7  8             FileOutputStream outputStream = new FileOutputStream(closeFile); //建立檔案輸出資料流 9             for (int i=0;i<files.length;i++){10                 FileInputStream inputStream = new FileInputStream(closeFile); //建立檔案輸入資料流11                 int len = (int) files[i].length(); //擷取單個子檔案大小12                 byte[] bytes = new byte[len];13                 inputStream.read(bytes);14                 outputStream.write(bytes);15                 inputStream.close();16             }17             outputStream.close();18         }catch (Exception e){19             e.printStackTrace();20         }21     }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.