Java--> multithreaded replication (file pointers)

Source: Internet
Author: User

--Here are two ways to use ... Actually not two kinds, just a little bit different ...

---> Test class

 Packagecom.dragon.java.multithreadcopy;ImportJava.io.File;ImportJava.util.Scanner;/** Copying files using multithreading 1*/ Public classTest { Public Static voidMain (string[] args) {@SuppressWarnings ("Resource") Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter the file path:"); File Srcfile=NewFile (Scanner.next ()); System.out.println ("Please enter the number of threads:"); intn =Scanner.nextint (); if(!srcfile.exists ()) {System.out.println ("The file does not exist!" "); } File desfile=NewFile (Srcfile.getparent (), "new" +srcfile.getname ());
     //Gets the size of the data to be copied by each thread from the number of threads
Longpartlenghth = Srcfile.length ()/n + 1; for(inti = 1; I < n + 1; i++) {
       //Start and end positions for each copy
NewMyThread (Srcfile, Desfile, partlenghth * (i-1), partlenghth*i). Start (); } }}

--Mythread class is the thread implementation class

 Packagecom.dragon.java.multithreadcopy;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.RandomAccessFile; Public classMyThreadextendsThread {PrivateFile srcfile; PrivateFile Desfile; Private Longstart; Private Longend;MyThread () {Super(); }
  //Construction method incoming source file, target file, this start position and end position MyThread (file srcfile, file Desfile,
LongStartLongend) { Super(); This. Srcfile =Srcfile; This. Desfile =Desfile; This. Start =start; This. end =end;} @Override Public voidrun () {randomaccessfile rafsrc=NULL; Randomaccessfile Rafdes=NULL; Try{rafsrc=NewRandomaccessfile (Srcfile, "R"); Rafdes=NewRandomaccessfile (Desfile, "RW");
       //Move the file pointer to the location where you want to start copying rafsrc.seek (start); Rafdes.seek (start);
intLen =-1; byte[] buffer =New byte[64]; while(len = rafsrc.read (buffer))! =-1) {rafdes.write (buffer,0, Len);
          jumps out of the loop when the read position is greater than or equal to the position to end if(Rafsrc.getfilepointer () >=end) { Break; } } } Catch(IOException e) {System.out.println (e); } }}

------------------------------------------------------------------------ The Evil dividing line----------------------------------------------------------------------------

--Test

ImportJava.util.Scanner;/** Use multi- threaded copy files to copy files using multithreading 2*/ Public classTest { Public Static voidMain (string[] args) {@SuppressWarnings ("Resource") Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter the file path:"); File Srcfile=NewFile (Scanner.next ()); System.out.println ("Please enter the number of threads:"); intn =Scanner.nextint (); if(!srcfile.exists ()) {System.out.println ("The file does not exist!" "); } File desfile=NewFile (Srcfile.getparent (), "new" +srcfile.getname ());
Longpartlenghth = Srcfile.length ()/n + 1; for(inti = 1; I < n + 1; i++) {
       
//The length of each incoming single-thread copy New MyThread (srcfile, Desfile, Partlenghth, Mythread.getpointer ()). Start (); } }}

--Mythread Thread implementation class

1  Packagecom.dragon.java.newthreadcopy;2 3 ImportJava.io.File;4 Importjava.io.IOException;5 ImportJava.io.RandomAccessFile;6 7  Public classMyThreadextendsThread {8     PrivateFile srcfile;9     PrivateFile Desfile;Ten     Private Longpartlength; One     Private Static Longpointer = 0; A  - MyThread () { -         Super(); the     } -  -MyThread (file srcfile, file Desfile,LongPartlength,Longpointer) { -         Super(); +          This. Srcfile =Srcfile; -          This. Desfile =Desfile; +          This. Partlength =partlength; AMythread.pointer =pointer; at     } -  - @Override -      Public voidrun () { -Randomaccessfile RAFSRC =NULL; -Randomaccessfile rafdes =NULL; in         Try { -RAFSRC =NewRandomaccessfile (Srcfile, "R"); toRafdes =NewRandomaccessfile (Desfile, "RW"); +  - Rafsrc.seek (pointer); the Rafdes.seek (pointer); * 
         //Copy full part length at once $ byte[] buffer =New byte[(int) partlength];Panax Notoginseng intLen =rafsrc.read (buffer); -Rafdes.write (buffer, 0, Len); thepointer =Rafsrc.getfilepointer (); +}Catch(IOException e) { A System.out.println (e); the } + } - $ Public Static LongGetpointer () { $ returnpointer; - } -}

--feeling the second is also completely redundant ah, is a way ...

Java--> multithreaded replication (file pointers)

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.