Java multi-thread file copying instance code

Source: Internet
Author: User

Copy codeThe Code is as follows: 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.tar getFileName = targetFile;
This. originFile = new RandomAccessFile (originFile), FileCoper. _ ORIGIN_FILE_MODE );
This.tar getFile = 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.tar getFileName = targetFile;
This. originFile = new RandomAccessFile (originFile), FileCoper. _ ORIGIN_FILE_MODE );
This.tar getFile = 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.tar getFileName = targetFile;
This. originFile = new RandomAccessFile (originFile), FileCoper. _ ORIGIN_FILE_MODE );
This.tar getFile = 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.tar getFileName = targetFile;
This. originFile = new RandomAccessFile (originFile), FileCoper. _ ORIGIN_FILE_MODE );
This.tar getFile = 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.tar getFile = targetFile;
This. threadCount = 1;
TotalThreadCount = this. threadCount;
}

Public void init (RandomAccessFile originFile, RandomAccessFile targetFile, int threadCount) throws Exception {
This. originFile = originFile;
This.tar getFile = targetFile;
This. threadCount = threadCount;
TotalThreadCount = this. threadCount;
}

Public static synchronized void finish (){
FileCoper.exe cutedCount ++;

System. out. println ("Total thread [" + FileCoper. totalThreadCount + "], copied to [" + FileCoper.exe cutedCount + "] threads !!! ");
If (FileCoper. totalThreadCount = FileCoper.exe cutedCount ){
Long time2 = System. currentTimeMillis ();
System. out. println ("Duration:" + (time2-time1 ));
System. out. println ("All [" + FileCoper. totalThreadCount + "] threads have been copied !!! ");
}
}

Public void start () throws Exception {
If (this. originFile. length () = 0)
Return;
If (this. threadCount = 0)
This. threadCount = 1;
// Set the target file size
This.tar getFile. setLength (this. originFile. length ());
This.tar getFile. seek (0 );
This. originFile. seek (0 );
Time1 = System. currentTimeMillis ();
System. out. println (this. originFile. length ());
// Block the file. Each block has a pair of values: the start position and end position 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: 1 cannot be added here. If 1 is added, garbled characters will occur when multiple files in the thread are added.
// Splits [I] [0] = startPos + 1;
Splits [I] [0] = startPos;
Splits [I] [1] = originFileLength;
} Else {
// Note: 1 cannot be added here. If 1 is added, garbled characters will occur when multiple files in the thread are added.
// 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.tar getFile, splits [I] [0], splits [I] [1]);
Fc. setOriginFileName (this. originFileName );
Fc.setTargetFileName(this.tar getFileName );
Fc. start ();
}
}

Public void startNew () throws Exception {
If (this. originFile. length () = 0)
Return;
// Set the target file size
This.tar getFile. setLength (this. originFile. length ());
This.tar getFile. 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.tar getFile, startPosition, endPosition );
Fc. setOriginFileName (this. originFileName );
Fc.setTargetFileName(this.tar getFileName );
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.tar getFileName = targetFileName;
}

Public Coper (String threadId ){
This. threadId = threadId;
}

Public void init (RandomAccessFile originFile, RandomAccessFile targetFile, long startPosition, long endPosition) throws Exception {
This. originFile = originFile;
This.tar getFile = targetFile;
This. startPosition = startPosition;
This. endPosition = endPosition;
This. blockCapacity = this. endPosition-this. startPosition;
}

Public void run (){
// System. out. println (this. threadId + "Start, Start copying the file [" +
// File block ["+ this. startPosition +", "+
// This. endPosition + "] to the target file [" + this.tar getFileName + ...");
Synchronized (this. originFile ){
Try {
// Record the number of currently copied bytes
Int copyCount = 0;
// Revelation offset of data copy
Long offSet = this. startPosition;
Byte [] B = new byte [16*1024*1024];
// Dynamically set the buffer of the number of bytes read at a time
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.tar getFile ){
Try {
If (copyCount = 0)
This.tar getFile. seek (offSet );
Else
This.tar getFile. seek (offSet + 1 );

This.tar getFile. write (B, 0, count );
} Catch (IOException e ){
E. printStackTrace ();
}
}
// Increase the number of copied bytes
CopyCount + = count;
// Copy actually [offset down
OffSet + = count;
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
// System. out. println (this. threadId + "copy file [" + this. originFileName
// + "] File block [" + this. startPosition + "," + this. endPosition +
// "] The target file [" + this.tar getFileName + "] is complete! ");

// Notify the main thread that the current thread completes the Replication
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 \ Make your lover", "d:/make your lover _5.mp3", 10 );
// Fc. init ("E: \ movie \ blackest hacker. rmvb", "d:/blackest hacker _ 1. rmvb", 100 );

/* // Read the 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, thread count ]");
System. out. print ("source file to be copied :");
OriginFile = br. readLine ();
System. out. print ("copy file to target file :");
TargetFile = br. readLine ();
System. out. print ("Number of splitting threads :");
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 );
}
}

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.