Java--> using file pointers to split files

Source: Internet
Author: User

--There's no difference in the way the byte-stream is divided, just adding the file pointer determines where to start the split ...

 PackageCom.dragon.java.splitmp3;ImportJava.io.File;ImportJava.io.RandomAccessFile;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args)throwsException {Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter the path of the file to be split:"); String FilePath=Scanner.next (); File Srcfile=NewFile (FilePath); System.out.println ("Please enter the number of copies to split:"); intn =Scanner.nextint (); Longpointer = 0; //divided into n segments, length of each segment, plus 1 to prevent data loss        LongParentlength = Srcfile.length ()/n + 1; Randomaccessfile rafsrc=NewRandomaccessfile (Srcfile, "R");  for(inti = 1; I < n + 1; i++) {            //the name of the segmented sub-fileRandomaccessfile Rafdec =NewRandomaccessfile (NewFile (Srcfile.getparent (),"Part" + i + "." + Srcfile.getname (). split ("\ \") [1]), "RW"); //point the file pointer to the position of the pointer when the last split was completedRafsrc.seek (pointer); byte[] buffer =New byte[(int) Parentlength]; intLen =rafsrc.read (buffer); Rafdec.write (Buffer,0, Len); Pointer=Rafsrc.getfilepointer (); }    }}

--this time the partition of a MP3 file, unexpectedly found that the child files are valid, so that the audio file is stored in the same way as pictures and video files ...

Java--> using file pointers to split files

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.