Recently in the study of big data related knowledge points, which need to implement file slicing and merging, complete Java implementation code, the following posted personal code, for reference only
First build a Splitutil tool class, there are three methods in the tool class Getsplitfile,getwrite,merge
1, File split code
Public Static voidGetsplitfile (String file,intcount) { //pre-allocated file takes up disk space, creates a file of a specified size on disk, "R" indicates read-only, "RW" supports random Read and write Try{Randomaccessfile RAF=NewRandomaccessfile (NewFile (file), "R"); //Calculate File Size LongLength =raf.length (); System.out.println (length); //calculate the size of each file after a file slice LongMaxSize = length/count; System.out.println (maxSize); Longoffset = 0L;//defines the offset of the initial file (read progress)//Start cutting Files for(inti = 0; i < count-1; i++) {//Count-1 The last document is not processed//Tag Initialization LongFbegin =offset; //split the first several files LongFend = (i+1) *maxSize; //Write Fileoffset =getwrite (file, I, Fbegin, fend); } //The remainder of the file is written to the last copy (if not evenly distributed) if(Length-offset > 0) { //Write FileGetwrite (file, count-1, offset, length); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }
2,getwrite File Write code
/*** Specify the boundaries of each copy of the file and write to different files *@paramFile source Files *@paramIndex source file order identification *@paramBegin The position of the pointer *@paramend position of the pointer *@returnLong*/ Public Static LongGetwrite (String file,intIndexLongBeginLongend) { LongEndpointer = 0L; Try { //declare the file disk after the file is cutRandomaccessfile in =NewRandomaccessfile (NewFile (file), "R"); //defines a readable, writable file and a binary file with a suffix of. tmpRandomaccessfile out =NewRandomaccessfile (NewFile (file + "_" + Index + ". tmp"), "RW"); //declare a byte array for each individual file byte[] B =New byte[1024]; intn = 0; //reads a file byte stream from a specified locationIn.seek (begin); //determine the boundary of a file stream read while(In.getfilepointer () <= end && (n = in.read (b))! =-1){ //writes a different file from the specified range of filesOut.write (b, 0, N); } //defines a pointer to the currently read fileEndpointer =In.getfilepointer (); //close the input streamIn.close (); //turn off the output streamOut.close (); } Catch(Exception e) {e.printstacktrace (); } returnEndpointer; }
3. File Merge Code
/*** File Merging *@paramfile specifies merge files *@paramtempfile file name before partition *@paramnumber of Tempcount files*/ Public Static voidMerge (String file,string tempfile,intTempcount) {Randomaccessfile RAF=NULL; Try { //declare random Read file RandomaccessfileRAF =NewRandomaccessfile (NewFile (file), "RW"); //start merging files, corresponding slices of binary files for(inti = 0; i < Tempcount; i++) { //reading a slice fileRandomaccessfile reader =NewRandomaccessfile (NewFile (Tempfile + "_" + i + ". tmp"), "R"); byte[] B =New byte[1024]; intn = 0; while((n = reader.read (b))! =-1) {Raf.write (b,0, N);//while reading, writing on the side } } } Catch(Exception e) {e.printstacktrace (); }finally{ Try{raf.close (); } Catch(IOException e) {e.printstacktrace (); } }
The last call in the main program will do.
public static void main (string[] args) {String file = "F:\\java-study\\img\\mv.jpg" ; int count = 5; // 1. Writing file slicing tool classes based on existing file files // 2. Writing to a binary temporary file // Getsplitfile (file, count); // 3. Merges the specified number of files according to actual requirements String Tempfile = "F:\\java-study\\img\\img.jpg" ; Merge (file, Tempfile, 5
The above code can achieve the image, document, Mp3,mp4 and other file splitting and merging, the following is the picture slice and split
Complete file slicing and merging Java implementation code