java多線程複製檔案的執行個體代碼

來源:互聯網
上載者:User

複製代碼 代碼如下: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("匯流排程【" + FileCoper.totalThreadCount + "】,已經完成【" + FileCoper.executedCount + "】個線程的複製!!!");
if (FileCoper.totalThreadCount == FileCoper.executedCount){
long time2 = System.currentTimeMillis();
System.out.println("花費時間長度:"+(time2-time1));
System.out.println("所有【" + FileCoper.totalThreadCount + "】線程複製完成!!!");
}
}

public void start() throws Exception {
if (this.originFile.length() == 0)
return;
if (this.threadCount == 0)
this.threadCount = 1;
// 設定目標檔案大小
this.targetFile.setLength(this.originFile.length());
this.targetFile.seek(0);
this.originFile.seek(0);
time1 = System.currentTimeMillis();
System.out.println(this.originFile.length());
// 把檔案分塊,每一塊有一對值:當前塊在檔案中的起始位置和結束位置
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) {
// 注意:此處不能加1,如果加1,線程多檔案就會出現亂碼
// splits[i][0] = startPos + 1;
splits[i][0] = startPos;
splits[i][1] = originFileLength;
} else {
// 注意:此處不能加1,如果加1,線程多檔案就會出現亂碼
// 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;
// 設定目標檔案大小
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++) {
// 定義每次轉移的長度
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 Exception {
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 + " 啟動,開始複製檔案【" +
// this.originFileName + "】中的檔案塊【" + this.startPosition + "," +
// this.endPosition + "】到目標檔案【" + this.targetFileName + "】中...");
synchronized (this.originFile) {
try {
// 記錄當前拷貝的位元組數
int copyCount = 0;
// 資料拷貝的啟示位移量
long offSet = this.startPosition;
byte[] b = new byte[16 * 1024 * 1024];
// 動態設定一次讀取的位元組數緩衝
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();
}
}
// 增加拷貝的位元組數
copyCount += count;
// 拷貝其實【位移量下移
offSet += count;
}
} catch (IOException e) {
e.printStackTrace();
}
}
// System.out.println(this.threadId + " 複製檔案【" + this.originFileName
// + "】中的檔案塊【" + this.startPosition + "," + this.endPosition +
// "】到目標檔案【" + this.targetFileName + "】完成!");

// 通知主線程,當前線程完成複製工作
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\做你的愛人.mp3", "d:/做你的愛人_5.mp3", 10);
//fc.init("E:\電影\最黑暗侵襲.rmvb", "d:/最黑暗侵襲_1.rmvb", 100);

/* // 讀入鍵盤輸入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 檔案來源
String originFile;
// 檔案目標
String targetFile;
System.out.println("【源檔案、目標檔案、線程數】");
System.out.print("要複製的源檔案:");
originFile = br.readLine();
System.out.print("檔案複製到目標檔案:");
targetFile = br.readLine();
System.out.print("切分線程數:");
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);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.