clase pública FileCoper {
Cadena final estática privada _ORIGIN_FILE_MODE = "r";
Cadena final estática privada _TARGET_FILE_MODE = "rw";
estática privada durante mucho tiempo1 = 0l;
cadena privada origenFileName;
cadena privada targetFileName;
archivo de origen de RandomAccessFile privado;
archivo de destino privado RandomAccessFile;
privado int threadCount;
privado estático int totalThreadCount = 0;
int estático privado ejecutadoCount = 0;
FileCoper público() {
this.threadCount = 1;
totalThreadCount = this.threadCount;
}
public FileCoper(String origenFile, String targetFile) {
intentar {
this.originFileName = origenFile;
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;
} captura (FileNotFoundException e) {
e.printStackTrace();
}
}
public FileCoper(String origenFile, String targetFile, int threadCount) {
intentar {
this.originFileName = origenFile;
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;
} captura (FileNotFoundException e) {
e.printStackTrace();
}
}
public void init (String origenFile, String targetFile) lanza una excepción {
this.originFileName = origenFile;
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) lanza una excepción {
this.originFileName = origenFile;
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) lanza una excepción {
this.originFile = origenFile;
this.targetFile = targetFile;
this.threadCount = 1;
totalThreadCount = this.threadCount;
}
public void init(RandomAccessFile originFile, RandomAccessFile targetFile, int threadCount) lanza una excepción {
this.originFile = origenFile;
this.targetFile = targetFile;
this.threadCount = threadCount;
totalThreadCount = this.threadCount;
}
acabado vacío sincronizado estático público () {
FileCoper.executedCount ++;
System.out.println("总线程【" + FileCoper.totalThreadCount + "】,已经完成【" + FileCoper.executedCount + "】个线程的复制!!!");
si (FileCoper.totalThreadCount == FileCoper.executedCount){
mucho tiempo2 = System.currentTimeMillis();
System.out.println("花费时长:"+(tiempo2-tiempo1));
System.out.println("所有【" + FileCoper.totalThreadCount + "】线程复制完成!!!");
}
}
inicio público vacío () lanza una excepción {
si (this.originFile.length() == 0)
devolver;
si (this.threadCount == 0)
this.threadCount = 1;
// 设置目标文件大小
this.targetFile.setLength(this.originFile.length());
this.targetFile.seek(0);
this.originFile.seek(0);
tiempo1 = System.currentTimeMillis();
System.out.println(this.originFile.length());
// 把文件分块,每一块有一对值:当前块在文件中的起始位置和结束位置
long[][] divisiones = new long[this.threadCount][2];
long origenFileLength = this.originFile.length();
intPosInicio = 0;
para (int i = 0; i < this.threadCount; i++) {
divisiones[i][0] = 0;
divisiones[i][1] = 0;
si (yo == 0) {
divisiones[i][0] = 0;
divisiones[i][1] = originFileLength / this.threadCount;
} más si (i == this.threadCount - 1) {
// 注意:此处不能加1,如果加1,线程多文件就会出现乱码
// divide[i][0] = startPos + 1;
divisiones[i][0] = startPos;
divisiones[i][1] = origenFileLength;
} demás {
// 注意:此处不能加1,如果加1,线程多文件就会出现乱码
// divide[i][0] = startPos + 1;
divisiones[i][0] = startPos;
divisiones[i][1] = startPos + originFileLength / this.threadCount;
}
startPos += originFileLength / this.threadCount;
// System.out.println(divisiones[i][0] + " " + divisiones[i][1]);
Coper fc = new Coper("hilo-" + i);
fc.init(this.originFile, this.targetFile, divisiones[i][0], divisiones[i][1]);
fc.setOriginFileName(this.originFileName);
fc.setTargetFileName(this.targetFileName);
fc.start();
}
}
public void startNew() lanza una excepción {
si (this.originFile.length() == 0)
devolver;
// 设置目标文件大小
this.targetFile.setLength(this.originFile.length());
this.targetFile.seek(0);
this.originFile.seek(0);
posición de inicio larga;
posición final larga;
bloque largo = this.originFile.length() / 1029;
si (bloque <= 1)
this.threadCount = 1;
para (int i = 0; i < this.threadCount; i++) {
// 定义每次转移的长度
startPosition = i * 1029 * (bloque/this.threadCount);
endPosition = (i + 1) * 1029 * (bloque/this.threadCount);
si (i == (this.threadCount - 1))
endPosition = this.originFile.length();
Coper fc = new Coper("hilo-" + i);
fc.init(this.originFile, this.targetFile, startPosition, endPosition);
fc.setOriginFileName(this.originFileName);
fc.setTargetFileName(this.targetFileName);
fc.start();
}
}
clase privada Coper extiende Thread {
cadena privada origenFileName;
cadena privada targetFileName;
archivo de origen de RandomAccessFile privado;
archivo de destino privado RandomAccessFile;
ID de hilo de cadena privada;
posición de inicio larga privada;
posición final larga privada;
capacidad de bloque largo privado;
public void setOriginFileName(String origenFileName) {
this.originFileName = origenFileName;
}
public void setTargetFileName(String targetFileName) {
this.targetFileName = targetFileName;
}
Cobre público (String threadId) {
this.threadId = hiloId;
}
public void init(RandomAccessFile originFile, RandomAccessFile targetFile, posición de inicio larga, posición final larga) lanza una excepción {
this.originFile = origenFile;
this.targetFile = targetFile;
this.startPosition = startPosition;
this.endPosition = posición final;
this.blockCapacity = this.endPosition - this.startPosition;
}
ejecución pública vacía() {
// System.out.println(this.threadId + " 启动,开始复制文件【" +
// this.originFileName + "】中的文件块【" + this.startPosition + "," +
// this.endPosition + "】到目标文件【" + this.targetFileName + "】中...");
sincronizado (this.originFile) {
intentar {
// 记录当前拷贝的字节数
int copyCount = 0;
// 数据拷贝的启示偏移量
offSet largo = this.startPosition;
byte[] b = nuevo byte[16 * 1024 * 1024];
// 动态设置一次读取的字节数缓冲
tamaño de bloque largo = 0;
mientras (copyCount < this.blockCapacity) {
this.originFile.seek(offSet);
si (this.blockCapacity - copyCount > 16 * 1024 * 1024)
tamaño de bloque = 16 * 1024 * 1024;
demás
blockSize = this.blockCapacity - copyCount;
si (tamaño de bloque > this.blockCapacity - copyCount)
blockSize = this.blockCapacity - copyCount;
int recuento = this.originFile.read(b, 0, (int) blockSize);
sincronizado (este.targetFile) {
intentar {
si (copiaContar == 0)
this.targetFile.seek(offSet);
demás
this.targetFile.seek(offSet + 1);
this.targetFile.write(b, 0, recuento);
} captura (IOException e) {
e.printStackTrace();
}
}
// 增加拷贝的字节数
copyCount += recuento;
// 拷贝其实【偏移量下移
offSet += recuento;
}
} captura (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) lanza una excepción {
FileCoper fc = nuevo 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 = nuevo BufferedReader (nuevo InputStreamReader (System.in));
// 文件来源
Archivo de origen de cadena;
// 文件目标
Archivo de destino de cadena;
System.out.println("【源文件、目标文件、线程数】");
System.out.print("要复制的源文件:");
origenFile = br.readLine();
System.out.print("文件复制到目标文件:");
targetFile = br.readLine();
System.out.print("切分线程数:");
int threadCount = Integer.parseInt(br.readLine());
fc.init(archivoorigen, archivodestino, threadCount);*/
// fc.startNew();
mucho tiempo1 = System.currentTimeMillis();
fc.start();
mucho tiempo2 = System.currentTimeMillis();
System.out.println(tiempo2-tiempo1);
}
}