Simple file splitting and merging using Java

Source: Internet
Author: User
The main application is Io's randomaccessfile (I heard that resumable upload is also implemented using it)
Import java. Io .*;

Class Fen {
String filename;
Int size;

Fen (string filename, string size ){
This. filename = filename;
This. size = integer. parseint (size) * 1024;
}

Public void cut () throws exception {
Int Maxx = 0;
File infile = new file (filename );

Int filelength = (INT) infile. Length (); // get the file size
Int value; // gets the number to be split.

Randomaccessfile inn = new randomaccessfile (infile, "R"); // open the file to be split


Value = filelength/size;

Int I = 0;
Int J = 0;

// Output the file according to the number to be split
For (; j <value; j ++ ){
File OUTFILE = new file (infile. getname () + J + "zzii ");
Randomaccessfile outt = new randomaccessfile (OUTFILE, "RW ");
Maxx + = size;
For (; I <Maxx; I ++ ){
Outt. Write (Inn. Read ());
}
Outt. Close ();
}
File OUTFILE = new file (infile. getname () + J + "zzii ");
Randomaccessfile outt = new randomaccessfile (OUTFILE, "RW ");
For (; I <filelength; I ++ ){
 
Outt. Write (Inn. Read ());
}
Outt. Close ();
 
Inn. Close ();
}
}

Class he {
String filename;
String filtername;

He (string filename, string filtername ){
This. filename = filename;
This. filtername = filtername;
}


Public void unite () throws exception {
String [] tt;
File infile = new file ("."); // file in the current directory
File OUTFILE = new file (filename); // get the output name
Randomaccessfile outt = new randomaccessfile (OUTFILE, "RW ");

// Obtain the qualified file name
Tt = infile. List (New filenamefilter (){
Public Boolean accept (File Dir, string name ){
String RR = new file (name). tostring ();
Return RR. endswith (filtername );
}
});
// Print the obtained file name
For (INT I = 0; I <TT. length; I ++ ){
System. Out. println (TT [I]);
}

// Open all the files and write them into one file.
For (INT I = 0; I <TT. length; I ++ ){
Infile = new file (TT [I]);
Randomaccessfile inn = new randomaccessfile (infile, "R ");
Int C;
While (C = Inn. Read ())! =-1)
Outt. Write (C );
}

Outt. Close ();
}
}

Public class test {
Public static void main (final string [] ARGs) throws exception {

If (ARGs. Length = 0 ){
Print ();
Return;
}
If (ARGs [0]. Equals ("-c ")){
Fen cutt = new Fen (ARGs [1], argS [2]);
Cutt. Cut ();
}
Else if (ARGs [0]. Equals ("-R ")){
He Hee = new he (ARGs [1], argS [2]);
Hee. Unite ();
}
Else
Print ();

}
 
Public static void print (){
System. out. println ("Usage:/N points: Java test-C file1 size (unit: K)/n in combination with Java test-r file2 zzii (easy identification I set )");
}
}

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.