Java implementation to cut a large file into n fixed-size files

Source: Internet
Author: User

    //Java implementation to cut a large file into n fixed-size files     Packagecom.johnny.test; ImportJava.io.BufferedReader; ImportJava.io.File; ImportJava.io.FileInputStream; ImportJava.io.FileOutputStream; ImportJava.io.FileReader; Importjava.io.IOException;  Public classFengefile { Public Static FinalString SUFFIX = ". txt";//filename suffix after splitting//splits the specified file by the number of bytes of the given file, where name refers to the name of the file that needs to be split, and size refers to the specified small file.             Public StaticString[] Divide (String name,LongSizethrowsException {File file=NewFile (name); if(!file.exists () | | (!file.isfile ())) {                             Throw NewException ("The specified file does not exist!")                     ”); }                     //to get the parent file of the split file, the small file will be divided into this directoryFile Parentfile =File.getparentfile (); //get the size of the file                    LongFilelength =file.length (); SYSTEM.OUT.PRINTLN ("File size:"+filelength+"bytes"); if(Size <= 0) {size= FILELENGTH/2; }                     //The number of small files that have been split                    intnum = (filelength% size! = 0)? (int) (Filelength/size + 1)                                     : (int) (Filelength/size); //Store the small file name after being splitstring[] FileNames =NewString[num]; //input file stream, which is the split fileFileInputStream in =Newfileinputstream (file); //read the start and end subscript of the input file stream                    LongEnd = 0; intBegin = 0; //output files based on the number of partitions to be split                     for(inti = 0; i < num; i++) {                             //for the first num–1 small file, the size is specifiedFile OutFile =NewFile (Parentfile, file.getname () + i +SUFFIX); //build a small file output streamFileOutputStream out =NewFileOutputStream (OutFile); //end subscript to move sizeEnd + =size; End= (End > Filelength)?Filelength:end; //Read bytes from the input stream are stored in the output stream                             for(; begin < End; begin++) {out.write (In.read ());                             } out.close (); Filenames[i]=Outfile.getabsolutepath ();                     } in.close (); returnFileNames; }              Public Static voidReadfilemessage (String fileName) {//read out the contents of a small file that is split intoFile File =NewFile (fileName); BufferedReader Reader=NULL; Try{Reader=NewBufferedReader (Newfilereader (file)); String String=NULL; //reads the content by line until NULL is read to indicate the end of the read file                             while(String = Reader.readline ())! =NULL) {System.out.println (string);                     } reader.close (); } Catch(IOException e) {e.printstacktrace (); } finally {                             if(Reader! =NULL) {                                     Try{reader.close (); } Catch(IOException E1) {}}} }              Public Static voidMainFinalString[] args)throwsException {String name= "d:/boss/123. txt "; Longsize = 1024*1024*4;//1k=1024b (bytes)string[] FileNames =fengefile.divide (name, size); System.out.println ("File"+ name +"The result of the segmentation is as follows:");  for(inti = 0; i < filenames.length; i++) {System.out.println (Filenames[i]+"The contents are as follows:"); //fengefile.readfilemessage (Filenames[i]);System.out.println (); }             }     } 

Java implementation to cut a large file into n fixed-size files

Related Article

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.