classe publique FileCoper {
chaîne finale statique privée _ORIGIN_FILE_MODE = "r";
chaîne finale statique privée _TARGET_FILE_MODE = "rw";
privé statique long time1 = 0l ;
chaîne privée originFileName ;
chaîne privée targetFileName ;
fichier d'origine RandomAccessFile privé ;
fichier cible RandomAccessFile privé ;
nombre de threads int privé ;
private static int totalThreadCount = 0 ;
private static int exécutéCount = 0 ;
public FileCoper() {
this.threadCount = 1 ;
totalThreadCount = this.threadCount ;
}
public FileCoper (String originFile, String targetFile) {
essayer {
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) {
essayer {
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) lève une 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) lance une 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) lève une exception {
this.originFile = originFile;
this.targetFile = targetFile ;
this.threadCount = 1 ;
totalThreadCount = this.threadCount ;
}
public void init (RandomAccessFile originFile, RandomAccessFile targetFile, int threadCount) lance une exception {
this.originFile = originFile;
this.targetFile = targetFile ;
this.threadCount = threadCount;
totalThreadCount = this.threadCount ;
}
finition vide synchronisée statique publique() {
FileCoper.executedCount ++;
System.out.println("总线程【" + FileCoper.totalThreadCount + "】,已经完成【" + FileCoper.executedCount + "】个线程的复制!!!");
si (FileCoper.totalThreadCount == FileCoper.executedCount){
long time2 = System.currentTimeMillis();
System.out.println("花费时长:"+(time2-time1));
System.out.println("所有【" + FileCoper.totalThreadCount + "】线程复制完成!!!");
}
}
public void start() lève une exception {
si (this.originFile.length() == 0)
retour;
si (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 = nouveau long[this.threadCount][2];
long originFileLength = this.originFile.length();
int startPos = 0;
pour (int i = 0; i < this.threadCount; i++) {
divise[i][0] = 0 ;
divise[i][1] = 0 ;
si (je == 0) {
divise[i][0] = 0 ;
splits[i][1] = originFileLength / this.threadCount ;
} sinon if (i == this.threadCount - 1) {
// 注意:此处不能加1,如果加1,线程多文件就会出现乱码
// divise[i][0] = startPos + 1 ;
splits[i][0] = startPos;
splits[i][1] = originFileLength;
} autre {
// 注意:此处不能加1,如果加1,线程多文件就会出现乱码
// divise[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() lance une exception {
si (this.originFile.length() == 0)
retour;
// 设置目标文件大小
this.targetFile.setLength(this.originFile.length());
this.targetFile.seek(0);
this.originFile.seek(0);
position de départ longue ;
position de fin longue ;
bloc long = this.originFile.length() / 1029;
si (bloc <= 1)
this.threadCount = 1 ;
pour (int i = 0; i < this.threadCount; i++) {
// 定义每次转移的长度
startPosition = i * 1029 * (bloc / this.threadCount);
endPosition = (i + 1) * 1029 * (bloc / this.threadCount);
si (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();
}
}
la classe privée Coper étend Thread {
chaîne privée originFileName ;
chaîne privée targetFileName ;
fichier d'origine RandomAccessFile privé ;
fichier cible RandomAccessFile privé ;
ID de fil de chaîne privé ;
position de départ longue privée ;
position de fin longue privée ;
Capacité de bloc longue privée ;
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) lève une 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 + "】中...");
synchronisé (this.originFile) {
essayer {
// 记录当前拷贝的字节数
int copyCount = 0 ;
// 数据拷贝的启示偏移量
long offSet = this.startPosition;
octet[] b = nouvel octet[16 * 1024 * 1024];
// 动态设置一次读取的字节数缓冲
taille de bloc longue = 0 ;
while (copyCount < this.blockCapacity) {
this.originFile.seek(offSet);
if (this.blockCapacity - copyCount > 16 * 1024 * 1024)
taille du bloc = 16 * 1024 * 1024 ;
autre
blockSize = this.blockCapacity - copyCount ;
if (blockSize > this.blockCapacity - copyCount)
blockSize = this.blockCapacity - copyCount ;
int count = this.originFile.read(b, 0, (int) blockSize);
synchronisé (this.targetFile) {
essayer {
si (copyCount == 0)
this.targetFile.seek(offSet);
autre
this.targetFile.seek(offSet + 1);
this.targetFile.write(b, 0, nombre);
} catch (IOException e) {
e.printStackTrace();
}
}
// 增加拷贝的字节数
copyCount += nombre ;
// 拷贝其实【偏移量下移
offSet += nombre ;
}
} 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) lève une exception {
FileCoper fc = nouveau 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));
// 文件来源
Chaîne OriginFile ;
// 文件目标
Chaîne 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);
}
}