How to split and merge files?

Source: Internet
Author: User

You can use the randomaccessfile class to split and merge files, which can be read and written anywhere. For example, multi‑thread download and resumable upload all require file splitting and merging.

The following is a simple example. For more information, see.

Public class implementsfilecutandunite {/***** @ Param filename source file * @ Param filterfolder the directory where the split file is located * @ Param size, in kb */Public void cut (string filename, string filterfolder, int size) {size = size * 1024; int maxsize = 0; file outfolder = new file (filterfolder ); if (! Outfolder. exists () {outfolder. mkdirs ();} file infile = new file (filename); // get the file size int filelength = (INT) infile. length (); // get the number of splits int value = 0; randomaccessfile inn = NULL; randomaccessfile outt = NULL; try {// open the file inn = new randomaccessfile (infile, "R"); value = filelength/size; int I = 0; Int J = 0; (; j <value; j ++) {file OUTFILE = new file (filterfolder + file. separator + infile. getname () + J + "tmp"); randomaccessfile out = new randomaccessfile (OUTFILE, "RW"); maxsize + = size; For (; I <maxsize; I ++) {out. write (Inn. read ();} Out. close ();} file OUTFILE = new file (filterfolder + file. separator + infile. getname () + J + "tmp"); outt = new randomaccessfile (OUTFILE, "RW"); For (; I <filelength; I ++) {outt. write (Inn. read () ;}} catch (filenotfoundexception e) {// todo auto-generated catch B Locke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {try {outt. close (); Inn. close ();} catch (ioexception e) {e. printstacktrace ();}}} /***** @ Param filename merged file * @ Param filterfolder directory where the split file is located * @ Param filtername the suffix of the split file * @ throws exception */Public void unite (string filename, string filterfolder, final string filtername) throws limit t Ion {file [] tt; file infile = new file (filterfolder); // file OUTFILE = new file (filename) in the current directory ); // obtain the output name randomaccessfile outt = new randomaccessfile (OUTFILE, "RW"); TT = infile. listfiles (New filenamefilter () {@ overridepublic Boolean accept (File Dir, string name) {string RR = new file (name ). tostring (); Return RR. endswith (filtername) ;}}); For (INT I = 0; I <TT. length; I ++) {system. out. println (TT [I]) ;}For (INT I = 0; I <TT. length; I ++) {randomaccessfile inn = new randomaccessfile (TT [I], "R"); int C = 0; while (C = Inn. read ())! =-1) {outt. Write (c) ;}} outt. Close ();}}

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.