An example of Java splitting a file into multiple subfiles and merging the subfiles into the original file
- 2020-06-03 06:29:32
- OfStack
Java's example of splitting a file into multiple subfiles and then merging the subfiles into the original file is as follows:
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
{
/*
* will 1 Three files split into multiple files , And then combine them again 1 A file
* Find the file , Read in 1 a 1M the buffer In the , Then write 1 a Part In the file , This operation is looped until the file is read
*/
String sourceFilePath = "D:" + File.separator + "Code" + File.separator + "source" + File.separator + "031316_ The first 13 Chapter: Java Class set 】 _ Properties: Properties.wmv";
int partFileLength = 1024*1024;// Specifies the size of the split subfile as 1M
splitFile(sourceFilePath,partFileLength);// Split the file
combineFile("D:" + File.separator + "Code" + File.separator + "target");// Merge the split files
}
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();// You need to keySet convert int type
// will keySet Iterative out , Converted to int The type of set, Sort it and store it in
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);
// Loop read file --> Write in turn
InputStream eachFileInput = null;
eachFileInput = new FileInputStream(new File(filePath));
byte[] buffer = new byte[1024*1024*1];// The buffer file size is 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;// This file stream is used to store information about the split file, including the number and path of each child file after the split , And the name of the file before the split
Properties partInfo = null;//properties Used to store information about file segmentation
byte[] buffer = null;
int partNumber = 1;
sourceFile = new File(sourceFilePath);// Split file
ips = new FileInputStream(sourceFile);// Find the read source file and get the input stream
configOps = new FileOutputStream(new File("D:" + File.separator + "Code" // The configuration file
+ File.separator + "target" + File.separator + "config.properties"));
buffer = new byte[partFileLength];// Open up cache space
int tempLength = 0;
partInfo = new Properties();//key:1 Start autonumbering value: The file path
while((tempLength = ips.read(buffer,0,partFileLength)) != -1)
{
String targetFilePath = "D:" + File.separator + "Code"
+ File.separator + "target" + File.separator + "part_" + (partNumber);// Split file path + The file name
partInfo.setProperty((partNumber++)+"",targetFilePath);// Store the relevant information in properties
targetFile = new File(targetFilePath);
ops = new FileOutputStream(targetFile);// Split file
ops.write(buffer,0,tempLength);// Writes the information to the shard file
ops.close();// Close the fragment file
}
partInfo.setProperty("name",sourceFile.getName());// Store source file name
partInfo.store(configOps,"ConfigFile");// will properties Store it in an entity file
ips.close();// Close the source file flow
}
}