JAVA-based multi-threaded file copying to improve file copy performance


Using the random access file RandomAccessFile in the IO stream and the file channel FileChannel to copy files can greatly improve the efficiency of reading and writing files, and on this basis, use multi-threading to copy files to make the performance better. Because the number of threads can be determined according to the size and needs of the file. The general principle is to segment the file according to the given number of threads, the size of data each thread is responsible for = file length/number of threads, and leave the indivisible part to the thread allocated by the last segment of the file for processing. The following is the implementation code and comments for your own understanding, please forgive me for any deviations. The following is some code

summed up : 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;//target file address

  private int start,end;//start specifies the start position, end specifies the end position

  //constructs the CopyThread method

  public CopyThread(StringsrcPath, String destPath, int start, int end) {

  this.srcPath = srcPath;//The path of the source file to be copied

  this.destPath = destPath;//The file path copied to this.start

  = start;//Copy start position

  this.end = end;//Copy end position

  }

  //Rewrite 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);// Jump the input to the specified position

  out.seek(start);// Write

  FileChannel from the specified position inChannel =in.getChannel(); //File input channel

  FileChannel outChannel =out.getChannel( );//File output channel

  //Lock the area that needs to be operated, false means to lock

  FileLock lock = outChannel.lock(start,(end-start), false);

  //Transfer bytes from this channel's file to the The specified outChannel channel that can write bytes.

  inChannel.transferTo(start,(end-start), outChannel);

  lock.release();//release the lock

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

  in.close();//close the file

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  Test class code:

  import java.io.File;

  public class TestMain {

  public static voidmain(String[] args) {

  //The path of the source file to be copied

  String srcPath ="F:\\sun\\class notes\\aa.txt";

  String destPath="F:\\sun\\class notes\\aa copy.txt";

  // Get the length of the source file

  File f = new File (srcPath);

  long len = f.length();

  int count = 3;//Number of threads required

  int oneNum = (int) (len /count);//The length of the file each thread is responsible for, cast to int Types of

  //Use the for loop to process the first and second parts of the divided file (the number of loops can be adjusted according to the number of threads defined)

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

  //oneNum * i starting 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 part of the file length that is not divisible Put it in the last paragraph to process

  CopyThread ct = newCopyThread(srcPath, destPath, oneNum * (count-1),(int)len);

  ct.start();

  }

  }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326525675&siteId=291194637