[Java] 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 Random AccessFile 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), FileCop Er. _ 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 Rand OmAccessFile (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. originF Ile = 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. totalThre AdCount + "], copied the [" + 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 + "] thread replication is complete !!! ") ;}} 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 with a pair of values: the start position and end position of the current block in the file long [] [] splits = new long [thi S. 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: you cannot add 1 here. If you add 1 to multiple files in a thread, garbled characters may occur. // splits [I] [0] = startPos + 1; splits [I] [0] = startPos; splits [I] [1] = origin FileLength;} else {// Note: you cannot add 1 here. If you add 1 to multiple files in the thread, garbled characters will appear. // 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. setOrigin FileName (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. threa DCount = 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. originFil EName); 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. startP Osition = startPosition; this. endPosition = endPosition; this. blockCapacity = this. endPosition-this. startPosition;} public void run () {// System. out. println (this. threadId + "Start, Start copying the file [" + // this. 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; // data Revelation offSet of copying long offSet = this. startPosition; byte [] B = new byte [16*1024*1024]; // dynamically sets the number of bytes read at a time. 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. blockCapac Ity-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 bytes to be copied copyCount + = count; // copy the [offSet down offSet + = count ;}} catch (IOException e) {e. printStackTrace ();} // System. out. println (this. threadId + "copy file [" + this. file block ["+ this. startPosition + "," + this. endPosition + // "] to the target file [" + this.tar getFileName +! "); // Notifies the main thread that the current thread completes the replication job 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 \ The blackest attack. rmvb "," d:/the blackest Attack _ 1. rmvb ", 100); // read the input BufferedReader br = new BufferedReader (new InputStreamReader (System. in); // file source String originFile; // file target String targetFile; www.2cto.com 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 (); www.2cto.com long time1 = System. currentTimeMillis (); fc. start (); long time2 = System. currentTimeMillis (); System. out. println (time2-time1 );}}