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];//開辟緩存空間
int tempLength = 0;
partInfo = new Properties();//key:1開始自動(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項(xiàng)目啟動(dòng)后立即關(guān)閉的問(wèn)題
這篇文章主要介紹了解決SpringBoot web項(xiàng)目啟動(dòng)后立即關(guān)閉的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java實(shí)現(xiàn)單鏈表反轉(zhuǎn)的多種方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)單鏈表反轉(zhuǎn)的多種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
MyBatis在Spring環(huán)境下的事務(wù)管理
MyBatis的設(shè)計(jì)思想很簡(jiǎn)單,可以看做是對(duì)JDBC的一次封裝,并提供強(qiáng)大的動(dòng)態(tài)SQL映射功能。這篇文章主要介紹了MyBatis在Spring環(huán)境下的事務(wù)管理 ,需要的朋友可以參考下2019-07-07
使用chatgpt實(shí)現(xiàn)微信聊天小程序的代碼示例
這篇文章主要介紹了使用chatgpt實(shí)現(xiàn)微信聊天小程序(秒回復(fù)),文中有詳細(xì)的代碼示例,對(duì)大家了解chatgpt聊天有一定的幫助,感興趣的同學(xué)可以參考閱讀2023-05-05
詳解SpringBoot 使用Spring Initializr 快速構(gòu)建工程(官方推薦)
本篇文章主要介紹了SpringBoot 使用Spring Initializr 快速構(gòu)建工程(官方推薦),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
Springboot項(xiàng)目中定時(shí)任務(wù)的四種實(shí)現(xiàn)方式詳解
Spring的@Scheduled注解是一種非常簡(jiǎn)單和便捷的實(shí)現(xiàn)定時(shí)任務(wù)的方式,通過(guò)在方法上添加@Scheduled注解,我們可以指定方法在特定的時(shí)間間隔或固定的時(shí)間點(diǎn)執(zhí)行,本文給大家介紹Springboot項(xiàng)目中定時(shí)任務(wù)的四種實(shí)現(xiàn)方式,感興趣的的朋友一起看看b2024-02-02

