国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java將文件分割為多個子文件再將子文件合并成原始文件的示例

2019-11-26 13:09:32
字體:
供稿:網(wǎng)友

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)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪洞县| 阜新市| 新建县| 读书| 宁阳县| 应城市| 九江县| 青岛市| 惠水县| 正宁县| 永仁县| 深水埗区| 丰台区| 佳木斯市| 湘西| 依兰县| 平利县| 射阳县| 吴旗县| 威海市| 东阿县| 双桥区| 胶州市| 洞头县| 宜丰县| 浙江省| 闸北区| 房产| 澎湖县| 遵化市| 剑河县| 婺源县| 锡林浩特市| 和田市| 乌兰县| 伊春市| 民丰县| 白银市| 丹阳市| 乌鲁木齐县| 辽源市|