Java將文件分割為多個子文件再將子文件合并成原始文件的示例
更新時間:2017年02月03日 15:53:03 作者:4舍318
本篇文章主要介紹了Java將文件分割為多個子文件再將子文件合并成原始文件的示例,具有一定的參考價值,有興趣的可以了解一下。
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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?Web請求響應(yīng)詳細(xì)代碼示例
在Web開發(fā)中請求和響應(yīng)是必不可少的環(huán)節(jié),Spring Boot Web應(yīng)用中請求響應(yīng)的分層解耦是構(gòu)建高效、可維護(hù)系統(tǒng)的關(guān)鍵實(shí)踐,下面這篇文章主要介紹了SpringBoot?Web請求響應(yīng)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
Java中計算集合中元素的出現(xiàn)次數(shù)統(tǒng)計
本文主要介紹了Java中計算集合中元素的出現(xiàn)次數(shù)統(tǒng)計,使用Collections類配合HashMap來統(tǒng)計和java lamb 計算這兩種方式,具有一定的參考價值,感興趣可以了解一下2024-02-02
MyBatis-Plus多數(shù)據(jù)源的示例代碼
本文主要介紹了MyBatis-Plus多數(shù)據(jù)源的示例代碼,包括依賴配置、數(shù)據(jù)源配置、Mapper 和 Service 的定義,具有一定的參考價值,感興趣的可以了解一下2024-05-05
Java中的ReadWriteLock高效處理并發(fā)讀寫操作實(shí)例探究
這篇文章主要為大家介紹了Java中的ReadWriteLock高效處理并發(fā)讀寫操作實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Java Enum和String及int的相互轉(zhuǎn)化示例
這篇文章主要介紹了Java Enum和String及int的相互轉(zhuǎn)化示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
MyBatis-Plus Sequence主鍵的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解
這篇文章主要為大家介紹了Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

