Java Foundation Multi-threaded copy file for improved file copy performance

Source: Internet
Author: User
Tags file copy

Using random Access file randomaccessfile and file channel Filechanne in IO stream to copy files can greatly improve the reading and writing efficiency of files, and make use of multi-thread to copy files to better performance. Depending on the size and requirements of the file, the number of threads can be determined. The approximate principle is that, depending on the number of threads given, the files are segmented, each thread is responsible for the size of the data = file length / number of threads, and the part that cannot be removed is left to the thread that is allocated for the last file. The following is the implementation code and their own understanding of the comments, there are many deviations please forgive me. Here are the brothers and even Java training

Summary of some of the code: for reference.

Program Implementation Class Code:

  Importjava.io.RandomAccessFile;

  Importjava.nio.channels.FileChannel;

  Importjava.nio.channels.FileLock;

  // define a Copythread class to inherit the Thread class

  public class Copythreadextends thread{

  Private String srcpath;// original file address

  Private String destpath;// destination file address

  private int Start,end;//start Specifies the starting position , end specifies the ending position

  // construction Copythread method

  Public Copythread (Stringsrcpath, String destpath, int start, int end) {

  "http://blog.51cto.com/viewpic.php?refimg=" + This.srcpath = srcpath;// The source file path to copy

  This.destpath = destpath;// copied to the file path

  This.start = start;// copy start position

  this.end = end;// Copy End location

  }

  // rewrite the run () method

  public void Run () {

  try {

  // Create a read-only random Access file

  Randomaccessfile in = Newrandomaccessfile (Srcpath, "R");

  // Create a readable and writable random Access file

  Randomaccessfile out = Newrandomaccessfile (DestPath, "RW");

  In.seek (start);// jumps the input to the specified position

  Out.seek (start);// write from specified position

  FileChannel Inchannel =in.getchannel ();// file input Channel

  FileChannel Outchannel =out.getchannel ();// file output channel

  // Lock the area that needs operation , false means lock

  Filelock lock = Outchannel.lock (Start, (End-start), false);

  // transfers bytes from this channel to the Outchannel channel of the given writable byte.

  Inchannel.transferto (Start, (End-start), Outchannel);

  lock.release ();// release lock

  out.close ();// close file from inside to outside

  in.close ();// Close File

  } catch (Exception e) {

  E.printstacktrace ();

  }

  }

  }

Test Class Code:

  Import Java.io.File;

  public class Testmain {

  public static Voidmain (string[] args) {

  // The source file path to copy

  String srcpath = "f:\\sun\\ class note \\aa.txt";

  String destpath = "f:\\sun\\ class note \\aa copy . txt";

  // Get source file length

  File F = new file (Srcpath);

  Long len = F.length ();

  int count = 3;// The number of threads required

  int onenum = (int) (len/count);// Thread-Responsible file length, cast to int type

  // use for loop to divide the first part of the file with the second part ( cycles can be adjusted according to the number of threads defined )

  for (int i = 0; I <count-1; i++) {

  //onenum * I start position, Onenum * (i + 1) The length of the data to be copied

  Copythread ct = newcopythread (Srcpath, DestPath, Onenum * i,onenum * (i + 1));

  Ct.start ();

  }

  // the portion of the file length cannot be divisible into the last paragraph of processing

  Copythread ct = newcopythread (Srcpath, DestPath, Onenum * (count-1), (int) len);

  Ct.start ();

  }

  }



Java Foundation Multi-threaded copy file for improved file copy performance

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.