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

Source: Internet
Author: User
Tags file copy

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

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 some of the code that brothers even Java training summarizes: for reference.

Program Implementation Class Code:

Importjava.io.RandomAccessFile;

Importjava.nio.channels.FileChannel;

Importjava.nio.channels.FileLock;

Defines a Copythread class that inherits 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

Constructing the 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

}

Override 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 to be operated on, false means lock

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

Transfers bytes from this channel's file 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 path of the source file 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 that cannot be divisible into the last paragraph is processed

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

Ct.start ();

}

}


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

Related Article

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.