Package com.test;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
public class Filecoper {
private static final String _origin_file_mode = "R";
private static final String _target_file_mode = "RW";
private static long time1 = 0l;
Private String Originfilename;
Private String TargetFileName;
Private Randomaccessfile Originfile;
Private Randomaccessfile targetfile;
private int threadcount;
private static int totalthreadcount = 0;
private static int executedcount = 0;
Public Filecoper () {
This.threadcount = 1;
Totalthreadcount = This.threadcount;
}
Public Filecoper (String originfile, String targetfile) {
try {
This.originfilename = Originfile;
This.targetfilename = TargetFile;
This.originfile = new Randomaccessfile ((originfile), filecoper._origin_file_mode);
This.targetfile = new Randomaccessfile ((targetfile), filecoper._target_file_mode);
This.threadcount = 1;
Totalthreadcount = This.threadcount;
catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
Public Filecoper (String originfile, string targetfile, int threadcount) {
try {
This.originfilename = Originfile;
This.targetfilename = TargetFile;
This.originfile = new Randomaccessfile ((originfile), filecoper._origin_file_mode);
This.targetfile = new Randomaccessfile ((targetfile), filecoper._target_file_mode);
This.threadcount = 1;
Totalthreadcount = This.threadcount;
catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
public void init (string originfile, String targetfile) throws Exception {
This.originfilename = Originfile;
This.targetfilename = TargetFile;
This.originfile = new Randomaccessfile ((originfile), filecoper._origin_file_mode);
This.targetfile = new Randomaccessfile ((targetfile), filecoper._target_file_mode);
This.threadcount = 1;
Totalthreadcount = This.threadcount;
}
public void init (string originfile, string targetfile, int threadcount) throws Exception {
This.originfilename = Originfile;
This.targetfilename = TargetFile;
This.originfile = new Randomaccessfile ((originfile), filecoper._origin_file_mode);
This.targetfile = new Randomaccessfile ((targetfile), filecoper._target_file_mode);
This.threadcount = ThreadCount;
Totalthreadcount = This.threadcount;
}
public void Init (Randomaccessfile originfile, Randomaccessfile targetfile) throws Exception {
This.originfile = Originfile;
This.targetfile = TargetFile;
This.threadcount = 1;
Totalthreadcount = This.threadcount;
}
public void Init (Randomaccessfile originfile, randomaccessfile targetfile, int threadcount) throws Exception {
This.originfile = Originfile;
This.targetfile = TargetFile;
This.threadcount = ThreadCount;
Totalthreadcount = This.threadcount;
}
public static synchronized void finish () {
Filecoper.executedcount + +;
System.out.println ("Bus" "+ Filecoper.totalthreadcount +" ", has completed" "+ Filecoper.executedcount +" "" Copy of the thread!!! ");
if (Filecoper.totalthreadcount = = Filecoper.executedcount) {
Long time2 = System.currenttimemillis ();
System.out.println ("Spend a long time:" + (time2-time1));
System.out.println ("All" + Filecoper.totalthreadcount + "" Thread copy complete!!! ");
}
}
public void Start () throws Exception {
if (this.originFile.length () = = 0)
Return
if (this.threadcount = 0)
This.threadcount = 1;
Set Target file size
This.targetFile.setLength (This.originFile.length ());
This.targetFile.seek (0);
This.originFile.seek (0);
time1 = System.currenttimemillis ();
System.out.println (This.originFile.length ());
Divide the file into chunks, each with a pair of values: the starting and ending positions of the current block in the file
Long[][] splits = new long[this.threadcount][2];
Long originfilelength = This.originFile.length ();
int startpos = 0;
for (int i = 0; i < This.threadcount; i++) {
Splits[i][0] = 0;
SPLITS[I][1] = 0;
if (i = = 0) {
Splits[i][0] = 0;
SPLITS[I][1] = Originfilelength/this.threadcount;
else if (i = = this.threadcount-1) {
Note: Here can not add 1, if plus 1, the thread multiple files will appear garbled
Splits[i][0] = startpos + 1;
Splits[i][0] = startpos;
SPLITS[I][1] = originfilelength;
} else {
Note: Here can not add 1, if plus 1, the thread multiple files will appear garbled
Splits[i][0] = startpos + 1;
Splits[i][0] = startpos;
SPLITS[I][1] = startpos + originfilelength/this.threadcount;
}
Startpos + = Originfilelength/this.threadcount;
System.out.println (Splits[i][0] + "" + splits[i][1]);
Coper FC = new Coper ("thread-" + i);
Fc.init (This.originfile, This.targetfile, splits[i][0], splits[i][1]);
Fc.setoriginfilename (This.originfilename);
Fc.settargetfilename (This.targetfilename);
Fc.start ();
}
}
public void StartNew () throws Exception {
if (this.originFile.length () = = 0)
Return
Set Target file size
This.targetFile.setLength (This.originFile.length ());
This.targetFile.seek (0);
This.originFile.seek (0);
Long startposition;
Long endposition;
Long block = This.originFile.length ()/1029;
if (Block <= 1)
This.threadcount = 1;
for (int i = 0; i < This.threadcount; i++) {
Define the length of each transfer
StartPosition = i * 1029 * (block/this.threadcount);
Endposition = (i + 1) * 1029 * (block/this.threadcount);
if (i = = (this.threadcount-1))
Endposition = This.originFile.length ();
Coper FC = new Coper ("thread-" + i);
Fc.init (This.originfile, This.targetfile, StartPosition, endposition);
Fc.setoriginfilename (This.originfilename);
Fc.settargetfilename (This.targetfilename);
Fc.start ();
}
}
Private class Coper extends Thread {
Private String Originfilename;
Private String TargetFileName;
Private Randomaccessfile Originfile;
Private Randomaccessfile targetfile;
Private String threadId;
Private long startposition;
Private long endposition;
Private long blockcapacity;
public void Setoriginfilename (String originfilename) {
This.originfilename = Originfilename;
}
public void Settargetfilename (String targetfilename) {
This.targetfilename = TargetFileName;
}
Public Coper (String threadId) {
This.threadid = threadId;
}
public void Init (Randomaccessfile originfile, Randomaccessfile targetfile, Long startposition, long endposition) throws E xception {
This.originfile = Originfile;
This.targetfile = TargetFile;
This.startposition = startposition;
This.endposition = endposition;
this.blockcapacity = this.endposition-this.startposition;
}
public void Run () {
System.out.println (This.threadid + "Start, copy file" "+
This.originfilename + "" In the File Block "" + this.startposition + "," +
This.endposition + "" to the target file "" + This.targetfilename + "" "...);
Synchronized (this.originfile) {
try {
Record the number of bytes in the current copy
int copycount = 0;
The revelation offset of data copy
Long offSet = this.startposition;
Byte[] B = new BYTE[16 * 1024 * 1024];
Dynamically set the number of bytes read buffer
Long blockSize = 0;
while (Copycount < this.blockcapacity) {
This.originFile.seek (OffSet);
if (This.blockcapacity-copycount > 16 * 1024 * 1024)
BlockSize = 16 * 1024 * 1024;
Else
BlockSize = This.blockcapacity-copycount;
if (BlockSize > This.blockcapacity-copycount)
BlockSize = This.blockcapacity-copycount;
int count = This.originFile.read (b, 0, (int) blockSize);
Synchronized (this.targetfile) {
try {
if (copycount = 0)
This.targetFile.seek (OffSet);
Else
This.targetFile.seek (OffSet + 1);
This.targetFile.write (b, 0, Count);
catch (IOException e) {
E.printstacktrace ();
}
}
Increase the number of bytes copied
Copycount + = count;
Copy actually "offset Move down
OffSet = count;
}
catch (IOException e) {
E.printstacktrace ();
}
}
System.out.println (this.threadid + "Copy file" "+ This.originfilename
+ "" "in the File Block" "+ this.startposition +", "+ This.endposition +
"" to the target file "+ This.targetfilename +" "Complete!";
Notifies the main thread that the current thread completes the replication work
Filecoper.finish ();
}
}
public static void Main (string[] args) throws Exception {
Filecoper FC = new Filecoper ();
Fc.init ("E:/initialdata_zhihuan.sql", "E:/initialdata_zhihuan2.sql", 30);
Fc.init ("D:/valueadd_11.txt", "D:/valueadd_111.txt", 100);
Fc.init ("D:\tools\music\ Be Your Lover. mp3", "d:/Be Your Lover _5.mp3", 10);
Fc.init ("E:\ movie", "the darkest invasion. rmvb", "d:/darkest attack _1.rmvb", 100);
*//Read keyboard input
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
File source
String Originfile;
File target
String targetfile;
System.out.println ("source file, target file, number of threads");
System.out.print ("source file to copy:");
Originfile = Br.readline ();
System.out.print ("file copied to destination file:");
TargetFile = Br.readline ();
System.out.print ("Splitting thread number:");
int threadcount = Integer.parseint (Br.readline ());
Fc.init (Originfile, TargetFile, threadcount);
Fc.startnew ();
Long time1 = System.currenttimemillis ();
Fc.start ();
Long time2 = System.currenttimemillis ();
System.out.println (TIME2-TIME1);
}
}