JAVA Basics multi-threaded copy file for improved file copy performance
Use random access to file randomaccessfile and file channels in IO streams filechanne Copying 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's the brother company .Java Training Summary of some of the code: for Reference.
Program Implementation Class Code:
Import java.io.RandomAccessFile;
Import java.nio.channels.FileChannel;
Import java.nio.channels.FileLock;
defines a copythread class that inherits the Thread class
public class Copythread extends 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 (string srcpath, 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 = new Randomaccessfile (srcpath, "r");
create a readable and writable random Access file
Randomaccessfile out = new Randomaccessfile (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 void main (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 = new Copythread (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 = new Copythread (srcpath, destpath, onenum * (count-1), (int) len);
Ct.start ();
}
}
Java Foundation multi-threaded copy file for improved file copy performance