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 ();}}