This article describes the cutting and merging operations of Java to implement large files. Share it for your reference, as follows:
Here we realize the cutting and merging of large files.
Cut by the specified number (such as cutting a file into 10 copies) or cut by the specified size (such as the maximum number of each copy is not exceeding 10M). Both methods are OK.
Here I'm just writing some simple code for you:
package io2;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import javax.swing.JFileChooser;public class FileSplitDemo {/*** Realize cutting and merging of large files. Cut by the specified number (such as cutting a file into 10 copies) or cut by the specified size (such as the maximum number of each copy is not exceeding 10M). Both methods are OK. */public static void main(String[] args) {JFileChooser jfc = new JFileChooser();// Select file in Swing// Select file int result = jfc.showOpenDialog(null);// Display frame is used to select file File file = null;// File to be cut File dest = null;// Destination file try {if (result == JFileChooser.APPROVE_OPTION) {// Select file// Cut file file = jfc.getSelectedFile();// User selected file dest = new File(file.getParent(), "spliFile");cutingFile(file, dest);// Cut method// 2 Merge(When running, directly merge the file fragments that were just cut)String fileName = file.getName();mergeDemo(dest, fileName);// Merge file}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private static void mergeDemo(File dest, String fileName)throws IOException {// Robustness protection (using File object to open the channel) if (!dest.exists()) {throw new RuntimeException("File does not exist");}// Use a file array to load all the files inside. File part[] = dest.listFiles();// Return an array of abstract pathnames, these path names represent files in the directory represented by this abstract pathname. if (parth.length == 0) {throw new RuntimeException("Fragment does not exist");}// y Use sequence streams to merge ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();// for (int i = 0; i < partth.length; i++) {// list.add(new FileInputStream(parth[i]));// This is not the case, the merged files are in disordered order // }for (int i = 0; i < partth.length; i++) {list.add(new FileInputStream(new File(dest, fileName + (i + 1)+ "part")));// Socketing technology, the order of files should be added the same as the original file}// Enumeration object interface Enumeration<FileInputStream> en = Collections.enumeration(list); SequenceInputStream sq = new SequenceInputStream(en);// Write to the new file FileOutputStream fou = new FileOutputStream(new File(dest, fileName));byte buf[] = new byte[1024];sq.read(buf);int len = 0;while ((len = sq.read(buf)) > 0) {fou.write(buf, 0, len);}fou.close();sq.close();}private static void cuttingFile(File source, File dest) {// Cut try {FileInputStream fis = new FileInputStream(source);if (!dest.exists()) {// File operation IO stream to determine whether the file exists. dest.mkdir();}byte buf[] = new byte[1024 * 1024];// 1Mfis.read(buf);int len = 0;int cout = 1;while ((len = fis.read(buf)) != -1) {// Use OUT stream to cut the file FileOutputStream fout = new FileOutputStream(new File(dest,source.getName() + (cout++) + "part"));fout.write(buf, 0, len);fout.close();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}File Cutting: Cut a file into multiple pieces, each piece of fragment should not exceed 1M in size. You can further expand the function: the file name, length before cutting, the number of pieces after cutting, file name and other information can be written to the first piece or use properties to write these to the configuration file.
File merge: Here is a simple assumption that the name of the File object and the original file that are merged are known. In fact, these can be completely alive. If you save this information in a fragmented file or configuration file, you can also use the file selection dialog box to read the user's choice.
For more information about Java algorithms, readers who are interested in this site can view the topics: "Summary of Java Files and Directory Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.