package cn.com;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.RandomAccessFile;//檔案的拆分和組合//步驟://1 依據源檔案大小和每塊的大小計算出塊數//2 將每一塊寫到一個對應的檔案public class FileSeparatorAndUnite {long rawFileSize;long blocksNumber;String rawFileName=null;String [] allPaths=null;public static void main(String[] args) { FileSeparatorAndUnite test=new FileSeparatorAndUnite(); //拆分檔案 String [] allPaths=test.separatorFile("F:\\2.jpg", 1024*10); //組合檔案 test.uniteFile(allPaths, "F:\\33.jpg");}/** * @param rawFilePath 待拆分檔案的路徑 * @param perBlockSize 拆分後每份的大小 * @return 各部分路徑的數組集合 */private String[] separatorFile(String rawFilePath,long perBlockSize){File rawFile=new File(rawFilePath);rawFileName=rawFile.getName();rawFileSize=rawFile.length();blocksNumber=getBlocksNumber(rawFileSize,perBlockSize);allPaths=new String[(int)blocksNumber];if (blocksNumber==1) {perBlockSize=rawFileSize;}long perPartSize=0;long perPartBeginPosition=0;String perPartPath=null;for (int i = 1; i <=blocksNumber; i++) {if (i<blocksNumber) {//每一塊的大小就為perBlockSizeperPartSize=perBlockSize;} else {//最後一塊的大小為總大小-該塊的起始位置 perPartSize=rawFileSize-perPartBeginPosition;}//得到每一塊的pathif (blocksNumber==1) {perPartPath=rawFilePath+".bat";} else {perPartPath=getPerPartFilePath(rawFilePath, i);}//操作每一塊//第一次調用時perPartBeginPosition當然為0writePerPartToFile(rawFilePath, perPartPath, perBlockSize, perPartBeginPosition); //計算每一塊的在原檔案中的起始位置.perPartBeginPosition=perPartBeginPosition+perPartSize;//儲存每一塊的路徑allPaths[i-1]=perPartPath;}return allPaths;}/** * @param rawFilePath 原檔案路徑 * @param perPartFilePath 每部分對應的路徑 * @param blockSize 每部分的塊大小 * @param beginPosition 每部分在原檔案中的開始位置 * @return * 在這裡 len=len-(int)(writedBytes-blockSize)有些不好理解 * 假設blockSize=2100,每次寫入1024,那麼兩次後還剩餘 * 2100-1024*2=52 * 如果再寫入1024,那麼就會報錯,超出了最大範圍. * 所以還能寫入的=blockSize-已經寫入的大小(writedBytes) * 因為在if語句前面已經有了指派陳述式writedBytes=writedBytes+len; * 所以需要:len=len-(int)(writedBytes-blockSize) * 其實代入:len=len-(int)(writedBytes+len-blockSize) * 可以得知:len=blockSize-writedBytes */public boolean writePerPartToFile(String rawFilePath,String perPartFilePath,long blockSize,long beginPosition){RandomAccessFile raf=null;FileOutputStream fos=null;byte [] buffer=new byte[8*1024];int len=0;long writedBytes=0;try {raf=new RandomAccessFile(rawFilePath, "r");raf.seek(beginPosition);fos=new FileOutputStream(perPartFilePath);while ((len=raf.read(buffer))!=-1) {if (writedBytes<blockSize) {writedBytes=writedBytes+len;if (writedBytes<=blockSize) {fos.write(buffer, 0, len);} else { len=len-(int)(writedBytes-blockSize); fos.write(buffer, 0, len);}} }} catch (Exception e) {try {if (fos!=null) {fos.close();}if (raf!=null) {raf.close();}} catch (Exception ex) {}return false;}return true;}/** * @param rawFileSize 原檔案大小 * @param perBlockSize 每塊的大小 * @return 拆分後塊數 */public long getBlocksNumber(long rawFileSize,long perBlockSize){if (rawFileSize<=perBlockSize) {return 1;} else { if (rawFileSize%perBlockSize==0) {return ((rawFileSize/perBlockSize)+1);} else { return rawFileSize/perBlockSize;}}}/** * @param rawFilePath 原檔案路徑 * @param blockNumer 當前塊號碼 * @return 當前塊對應的路徑 */public String getPerPartFilePath(String rawFilePath,int blockNumer){String perPartFilePath=rawFilePath+".part"+blockNumer;return perPartFilePath;}/** * @param partsPaths 各部分路徑 * @param unitedFilePath 合并後檔案路徑 */public void uniteFile(String [] partsPaths,String unitedFilePath){try {File perPartFile=null;File unitedFile=new File(unitedFilePath);FileOutputStream fos=new FileOutputStream(unitedFile);FileInputStream fis=null;for (int i = 0; i < partsPaths.length; i++) {perPartFile=new File(partsPaths[i]);fis=new FileInputStream(perPartFile);byte [] buffer=new byte[1024*8];int len=0;while ((len=fis.read(buffer))!=-1) {fos.write(buffer, 0, len);}}fis.close();fos.close();} catch (Exception e) {}}}