Import java.io.file;import java.io.fileinputstream;import java.io.filenotfoundexception;import java.io.FileOutputStream;import java.io.FilenameFilter;import java.io.SequenceInputStream; Import java.util.arraylist;import java.util.collections;import java.util.enumeration;import java.util.List;import java.util.Properties;/** * SplitFile.java * @author: <a href= "mailto:[email protected" >bylv</a> * @DATE:2015-9-7 09:27:43 * Copyright (C) 2011 ISoftStone *//** * function Description: * * @author: <a href= "mailto:[email protected" >bylv</a> * @DATE :2015-9-7 @TIME: Morning 09:27:43 */public class splitfile {private static final int size = 1024 * 1024;// defines the size of a single file here 1m/** * function Description: * * @param args * @author: <a href= "mailto:[email protected]" >bylv</a> * @throws Exception * @DATE:2015-9-7 @TIME: am 09:27:44 */public static void main (String[] args) throws Exception {// split// file file = new file ("d:/untitled. bmp");// splitfile (File);// merge File file = new file ("C:/parfiles"); Mergefile (File);} /** * Feature Description: Merge files * * @param file * @author: <a href= "Mailto:[email protected" >bylv</a> * @throws Exception * @throws filenotfoundexception * @DATE:2015-9-7 @TIME: Morning 09:47:31 */private static Void mergefile (File dir) throws Exception {// read the split information for the properties file file[] Files = dir.listfiles (New filenamefilter () {@Overridepublic boolean&Nbsp;accept (File dir, string name) {return name.endswith (". Properties");}); file file = files[0];// get information about the file properties pro = new properties (); Fileinputstream fis = new fileinputstream (file);p ro.load (FIS); String filename = pro.getproperty ("FileName"); Int splitcount = integer.valueof ( Pro.getproperty ("Partcount"));if (files.length != 1) {throw new exception (dir + ", this directory does not have a parsed properties file or not unique");} Get all fragmented files in this directory File[] partfiles = dir.listfiles (New filenamefilter () {@ Overridepublic boolean accept (file dir, string name) {return name.endsWith ( ". part");}); / the fragment file into the collection list<fileinputstream> al = new arraylist<fileinputstream> (); for (int i = 0; i < splitcount; i++) {try {al.add (new filEinputstream (Partfiles[i]));} catch (exception e) {// exception E.printstacktrace ();}} try {// build File Stream collection Enumeration<fileinputstream> en = collections.enumeration (AL);// Sequenceinputstream sis = new sequenceinputstream multiple stream synthesis sequences (en); Fileoutputstream fos = new fileoutputstream (New file (Dir, fileName)); byte[] b = new byte[1024];int len = 0;while ((Len = sis.read (b)) != -1) {fos.write (B, 0, len);} Fos.close (); Sis.close ();} catch (exception e) {e.printstacktrace ();}} /** * Feature Description: Split file * * @param file * @author: <a href= "mailto:[email protected]" >bylv</a> * @DATE:2015-9-7 @TIME: Morning 09:28:58 */private static void splitfile (File file) {try {fileinputstream fs = new&nBsp FileInputStream (file);// defines the buffer byte[] b = new byte[size]; fileoutputstream fo = null;int len = 0;int count = 0;/** * When cutting files, record cut the name of the file and the number of sub-files cut to facilitate merging * this information for a simple description, the use of key-value pairs in the way the Properties object */properties pro = new properties ();// defines the output folder path File dir = new file ("C:/ Parfiles ");// determines if the folder exists, does not exist then creates if (!dir.exists ()) {dir.mkdirs ();} Cutting file while ((Len = fs.read (b)) != -1) {fo = new FileOutputStream (New file (dir, (count++) + ". part"); Fo.write (B, 0, len); Fo.close ();} Save the cut file information to the properties Pro.setproperty ("Partcount", count + ");p Ro.setproperty (" FileName ", file.getname ()); Fo = new fileoutputstream (New file (dir, (count++) + ". Properties");// write to the properties file Pro.store (fo, "save file info "); Fo.close (); Fs.close ();} catch (exception e) {e.printstacktrace ();}}
Splitting and merging of large Java files