Java將文件分割為多個(gè)子文件再將子文件合并成原始文件的示例
Java將文件分割為多個(gè)子文件再將子文件合并成原始文件的示例,廢話不多說(shuō),代碼如下:
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 { /* *將一個(gè)文件分割為多個(gè)文件,然后再組合成一個(gè)文件 * 找到文件,讀入一個(gè)1M的buffer中,然后寫入一個(gè)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迭代出來(lái),轉(zhuǎn)換成int類型的set,排序后存儲(chǔ)進(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;//該文件流用于存儲(chǔ)文件分割后的相關(guān)信息,包括分割后的每個(gè)子文件的編號(hào)和路徑,以及未分割前文件名 Properties partInfo = null;//properties用于存儲(chǔ)文件分割的信息 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];//開(kāi)辟緩存空間 int tempLength = 0; partInfo = new Properties();//key:1開(kāi)始自動(dòng)編號(hào) 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)信息存儲(chǔ)進(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());//存儲(chǔ)源文件名 partInfo.store(configOps,"ConfigFile");//將properties存儲(chǔ)進(jìn)實(shí)體文件中 ips.close();//關(guān)閉源文件流 } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?Web請(qǐng)求響應(yīng)詳細(xì)代碼示例
在Web開(kāi)發(fā)中請(qǐng)求和響應(yīng)是必不可少的環(huán)節(jié),Spring Boot Web應(yīng)用中請(qǐng)求響應(yīng)的分層解耦是構(gòu)建高效、可維護(hù)系統(tǒng)的關(guān)鍵實(shí)踐,下面這篇文章主要介紹了SpringBoot?Web請(qǐng)求響應(yīng)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09SpringMVC框架如何與Junit整合看這個(gè)就夠了
這篇文章主要介紹了SpringMVC框架如何與Junit整合看這個(gè)就夠了,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Java中計(jì)算集合中元素的出現(xiàn)次數(shù)統(tǒng)計(jì)
本文主要介紹了Java中計(jì)算集合中元素的出現(xiàn)次數(shù)統(tǒng)計(jì),使用Collections類配合HashMap來(lái)統(tǒng)計(jì)和java lamb 計(jì)算這兩種方式,具有一定的參考價(jià)值,感興趣可以了解一下2024-02-02MyBatis-Plus多數(shù)據(jù)源的示例代碼
本文主要介紹了MyBatis-Plus多數(shù)據(jù)源的示例代碼,包括依賴配置、數(shù)據(jù)源配置、Mapper 和 Service 的定義,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05Java中的ReadWriteLock高效處理并發(fā)讀寫操作實(shí)例探究
這篇文章主要為大家介紹了Java中的ReadWriteLock高效處理并發(fā)讀寫操作實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Java Enum和String及int的相互轉(zhuǎn)化示例
這篇文章主要介紹了Java Enum和String及int的相互轉(zhuǎn)化示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06MyBatis-Plus Sequence主鍵的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解
這篇文章主要為大家介紹了Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11