Java將文件分割為多個子文件再將子文件合并成原始文件的示例,廢話不多說,代碼如下:
import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Iterator; import java.util.TreeSet; import java.util.Set; public class Test { public static void main(String[] args) throws Exception { /* *將一個文件分割為多個文件,然后再組合成一個文件 * 找到文件,讀入一個1M的buffer中,然后寫入一個Part文件中,循環(huán)此操作直到文件讀取完畢 */ String sourceFilePath = "D:" + File.separator + "Code" + File.separator + "source" + File.separator + "031316_【第13章:Java類集】_屬性類:Properties.wmv"; int partFileLength = 1024*1024;//指定分割的子文件大小為1M splitFile(sourceFilePath,partFileLength);//將文件分割 combineFile("D:" + File.separator + "Code" + File.separator + "target");//將分割后的文件合并 } public static void combineFile(String directoryPath) throws Exception { Properties config = new Properties(); InputStream ips = null; ips = new FileInputStream(new File(directoryPath + File.separator + "config.properties")); config.load(ips); Set keySet = config.keySet();//需要將keySet轉(zhuǎn)換為int型 //將keySet迭代出來,轉(zhuǎn)換成int類型的set,排序后存儲進(jìn)去 Set<Integer> intSet = new TreeSet<Integer>(); Iterator iterString = keySet.iterator(); while(iterString.hasNext()) { String tempKey = (String)iterString.next(); if("name".equals(tempKey)) {} else { int tempInt ; tempInt = Integer.parseInt(tempKey); intSet.add(tempInt); } } Set<Integer> sortedKeySet = new TreeSet<Integer>(); sortedKeySet.addAll(intSet); OutputStream eachFileOutput = null; eachFileOutput = new FileOutputStream(new File("D:" + File.separator + "Code" + File.separator + config.getProperty("name"))); Iterator iter = sortedKeySet.iterator(); while(iter.hasNext()) { String key = new String("" + iter.next()); if(key.equals("name")) {} else { //System.out.println("debug---"); String fileNumber = null; String filePath = null; fileNumber = key; filePath = config.getProperty(fileNumber); //循環(huán)讀取文件 --> 依次寫入 InputStream eachFileInput = null; eachFileInput = new FileInputStream(new File(filePath)); byte[] buffer = new byte[1024*1024*1];//緩沖區(qū)文件大小為1M int len = 0; while((len = eachFileInput.read(buffer,0,1024*1024*1)) != -1) { eachFileOutput.write(buffer,0,len); } eachFileInput.close(); } } eachFileOutput.close(); } public static void splitFile(String sourceFilePath,int partFileLength) throws Exception { File sourceFile = null; File targetFile = null; InputStream ips = null; OutputStream ops = null; OutputStream configOps = null;//該文件流用于存儲文件分割后的相關(guān)信息,包括分割后的每個子文件的編號和路徑,以及未分割前文件名 Properties partInfo = null;//properties用于存儲文件分割的信息 byte[] buffer = null; int partNumber = 1; sourceFile = new File(sourceFilePath);//待分割文件 ips = new FileInputStream(sourceFile);//找到讀取源文件并獲取輸入流 configOps = new FileOutputStream(new File("D:" + File.separator + "Code" //配置文件 + File.separator + "target" + File.separator + "config.properties")); buffer = new byte[partFileLength];//開辟緩存空間 int tempLength = 0; partInfo = new Properties();//key:1開始自動編號 value:文件路徑 while((tempLength = ips.read(buffer,0,partFileLength)) != -1) { String targetFilePath = "D:" + File.separator + "Code" + File.separator + "target" + File.separator + "part_" + (partNumber);//分割后的文件路徑+文件名 partInfo.setProperty((partNumber++)+"",targetFilePath);//將相關(guān)信息存儲進(jìn)properties targetFile = new File(targetFilePath); ops = new FileOutputStream(targetFile);//分割后文件 ops.write(buffer,0,tempLength);//將信息寫入碎片文件 ops.close();//關(guān)閉碎片文件 } partInfo.setProperty("name",sourceFile.getName());//存儲源文件名 partInfo.store(configOps,"ConfigFile");//將properties存儲進(jìn)實(shí)體文件中 ips.close();//關(guān)閉源文件流 } } 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選