Split and merge Java files (1)

Source: Internet
Author: User
Package cn.com; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. randomaccessfile; // file splitting and combination // steps: // 1 calculate the number of parts based on the source file size and each part size. // 2 write each part to a corresponding file public class fileseparatorandunite {long rawfilesize; long blocksnumber; string rawfilename = NULL; string [] allpaths = NULL; public static void main (string [] ARGs) {fileseparatorandunite test = new fileseparatorandunite ();/ /Split the file string [] allpaths = test. separatorfile ("F: \ 2.jpg", 1024*10); // composite file test. unitefile (allpaths, "F: \ 99.jpg ");} /*** @ Param rawfilepath path of the file to be split * @ Param perblocksize size of each part after splitting * @ return array set of each part path */private string [] separatorfile (string rawfilepath, long perblocksize) {file rawfile = new file (rawfilepath); rawfilename = rawfile. getname (); rawfilesize = rawfile. length (); blocksnumber = getblocksnumber (rawfil Esize, perblocksize); allpaths = new string [(INT) blocksnumber]; If (blocksnumber = 1) {perblocksize = rawfilesize;} Long perpartsize = 0; long perpartbeginposition = 0; string perpartpath = NULL; For (INT I = 1; I <= blocksnumber; I ++) {if (I <blocksnumber) {// the size of each block is perblocksizeperpartsize = perblocksize;} else {// the size of the last block is the total size-the starting position of the block perpartsize = rawfilesize-perpartbeginposition ;} // obtain each block's paticf (blocksnumber = 1) {perp Artpath = rawfilepath + ". bat ";} else {perpartpath = getperpartfilepath (rawfilepath, I);} // you can operate on each part. // when calling for the first time, perpartbeginposition is of course 0system. out. println ("start position of the block: perpartbeginposition =" + perpartbeginposition); writeperparttofile (rawfilepath, perpartpath, perpartsize, perpartbeginposition); // calculates the start position of each block in the original file. perpartbeginposition = perpartbeginposition + perpartsize; // Save the path of each block allpaths [I-1] = perpartpath; system. out. P Rintln ("the block size: perpartsize =" + perpartsize);} return allpaths ;} /*** @ Param rawfilepath original file path * @ Param perpartfilepath path corresponding to each part * @ Param blocksize block size of each part * @ Param beginposition start position of each part in the original file * @ return ** rafforreader. read (buffer, 0, everytimereadlen) * indicates that everytimereadlen bytes are read into the buffer */Public Boolean writeperparttofile (string rawfilepath, string perpartfilepath, long blocksize, long beginposition ){ Randomaccessfile rafforreader = NULL; randomaccessfile rafforwriter = NULL; Boolean iscontinuereading = true; // The number of bytes that should be read each time int everytimereadlen = 0; byte [] buffer = new byte [1024*8]; try {rafforreader = new randomaccessfile (rawfilepath, "R"); rafforreader. seek (beginposition); file perpartfile = new file (perpartfilepath); rafforwriter = new randomaccessfile (perpartfile, "RW"); // set the file size rafforwriter. setlength (blocksize); int Writeroff = 0; // set the bytes that the first read () should read if (blocksize> buffer. length) {everytimereadlen = buffer. length;} else {everytimereadlen = (INT) blocksize;} while (rafforreader. read (buffer, 0, everytimereadlen )! =-1 & iscontinuereading) {rafforwriter. seek (writeroff); writeroff + = everytimereadlen; rafforwriter. write (buffer, 0, everytimereadlen); // dynamically change the number of bytes to be read next time if (blocksize-writeroff> buffer. length) {everytimereadlen = buffer. length;} else {everytimereadlen = (INT) blocksize-writeroff;} // If (everytimereadlen = 0) {iscontinuereading = false;} rafforreader. close (); rafforwriter. close ();} catch (exception e) {return False;} return true;}/*** @ Param rawfilesize original file size * @ Param perblocksize size of each block * @ return number of splits */public long getblocksnumber (long rawfilesize, long perblocksize) {If (rawfilesize <= perblocksize) {return 1 ;}else {If (rawfilesize % perblocksize = 0) {return (rawfilesize/perblocksize );} else {return (rawfilesize/perblocksize) + 1 ;}}/ *** @ Param rawfilepath original file path * @ Param blocknumer current block number * @ return corresponding to the current block Path */Public String getperpartfilepath (string rawfilepath, int blocknumer) {string perpartfilepath = rawfilepath + ". part "+ blocknumer; return perpartfilepath;}/*** @ Param partspaths path * @ Param unitedfilepath: merged file path */Public void unitefile (string [] partspaths, string unitedfilepath) {try {file perpartfile = NULL; file unitedfile = new file (unitedfilepath); fileoutputstream Fos = new fileoutputstream (unitedfi Le); fileinputstream FCM = NULL; For (INT I = 0; I <partspaths. length; I ++) {perpartfile = new file (partspaths [I]); FCM = new fileinputstream (perpartfile); byte [] buffer = new byte [1024*8]; int Len = 0; while (LEN = Fi. read (buffer ))! =-1) {FOS. Write (buffer, 0, Len) ;}} FD. Close (); FOS. Close () ;}catch (exception e ){}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.