--Breakpoint Continuation: Just like the Thunder download file, stop the download or close the program, the next time you download from the last place to continue, instead of starting from scratch ...
--Randomaccessfile---pointer (file pointer)--Seek (move file pointer)--the breakpoint continues
PackageCom.dragon.java.downloadfile;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.RandomAccessFile;/*Breakpoint Continuation: It is very important to download software! --First time download 100 bytes--Second Download 101 bytes ... Find out where you left off the last time. Where was the last time it was downloaded? Note the location of the breakpoint------> requires a third-party file to specifically remember the location of the breakpoint*/ Public classTest { Public Static voidMain (String args[]) {File srcfile=NewFile ("D:/java4android/01_java archaeological/01_java archaeology. mp4"); File Desdir=NewFile ("F:/vidio"); Copyfiletodir (Srcfile, Desdir); } Public Static voidcopyfiletodir (file srcfile, file Desdir) {desdir.mkdirs (); //Creating a configuration fileFile ConfigFile =NewFile (Desdir, Srcfile.getname (). split ("\ \") [0] + ". config"); //Create a target fileFile Desfile =NewFile (Desdir, Srcfile.getname ()); if(!configfile.exists () &&desfile.exists ()) {System.out.println ("The file has been downloaded!" "); return; } randomaccessfile rafsrc=NULL; Randomaccessfile Rafdes=NULL; Randomaccessfile Rafconfig=NULL; Try{rafsrc=NewRandomaccessfile (Srcfile, "R"); Rafdes=NewRandomaccessfile (Desfile, "RW"); Rafconfig=NewRandomaccessfile (ConfigFile, "RW"); //set the destination file as long as the source filerafdes.setlength (Srcfile.length ()); //set the file length of the configuration to 8 to prevent the first download from having an EOF exceptionRafconfig.setlength (8); //continue downloading from the location where you last downloaded! Longpointer =Rafconfig.readlong (); System.out.println ("Downloaded:" + ((float) pointer/srcfile.length ())* 100 + "%"); Rafsrc.seek (pointer); Rafdes.seek (pointer); //single transmission length set a small point, so as to observe whether the breakpoint continues to pass byte[] buffer =New byte[32]; intLen =-1; //At the beginning of each copy, the pointer to the source file and the pointer to the target file must be read from the last disconnected position . while(len = rafsrc.read (buffer))! =-1) {rafdes.write (buffer,0, Len); //when the profile is written, each time the file pointer is moved to its original location-so that it will only store the first 8 bytesRafconfig.seek (0); //each copy of the sum, hurriedly record the location of the file pointer, in case the breakpoint continued to use. Rafconfig.writelong (Rafsrc.getfilepointer ()); } } Catch(IOException e) {System.out.println (e); } finally { Try{rafsrc.close (); Rafdes.close (); Rafconfig.close (); } Catch(IOException e) {System.out.println (e); } //Delete a profile after a stream is closedSystem.out.println ("Download successful! "); Configfile.delete (); } }}
--Copy to simulate a simple breakpoint continuation ...
Java--> Implementing a breakpoint continuation (download)