Java-->多线程复制(文件指针)

时间:2023-03-09 22:53:30
Java-->多线程复制(文件指针)

--> 这里用到两种方法...其实也不算两种,就一点点不一样而已...

---> Test 测试类

package com.dragon.java.multithreadcopy;

import java.io.File;
import java.util.Scanner; /*
* 利用多线程复制文件1
*/
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("请输入文件路径:");
File srcFile = new File(scanner.next());
System.out.println("请输入线程数:");
int n = scanner.nextInt();
if (!srcFile.exists()) {
System.out.println("该文件不存在!");
} File desFile = new File(srcFile.getParent(), "new" + srcFile.getName());

     // 从线程数得到每个线程要复制的数据大小
long partLenghth = srcFile.length() / n + 1;
for (int i = 1; i < n + 1; i++) {
       // 每次复制的开始和结束位置
new MyThread(srcFile, desFile, partLenghth * (i - 1), partLenghth
* i).start(); }
}
}

--> MyThread类即线程实现类

package com.dragon.java.multithreadcopy;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile; public class MyThread extends Thread {
private File srcFile;
private File desFile;
private long start;
private long end; MyThread() {
super();
}

   // 构造方法传入源文件、目标文件、本次开始位置以及结束位置
MyThread(File srcFile, File desFile, long start, long end) {
super();
this.srcFile = srcFile;
this.desFile = desFile;
this.start = start;
this.end = end;
} @Override
public void run() {
RandomAccessFile rafSrc = null;
RandomAccessFile rafDes = null;
try {
rafSrc = new RandomAccessFile(srcFile, "r");
rafDes = new RandomAccessFile(desFile, "rw");

       // 将文件指针移动到将要开始复制的位置
rafSrc.seek(start);
rafDes.seek(start); int len = -1;
byte[] buffer = new byte[64];
while ((len = rafSrc.read(buffer)) != -1) {
rafDes.write(buffer, 0, len);
          // 当读取到的位置大于或等于要结束的位置时跳出循环
if (rafSrc.getFilePointer() >= end) {
break;
}
}
} catch (IOException e) {
System.out.println(e);
}
} }

--> -----------------------------------------------------------------------邪恶的分割线----------------------------------------------------------------------------

--> Test测试

import java.util.Scanner;

/*
* 利用多线程复制文件利用多线程复制文件2
*/
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
System.out.println("请输入文件路径:");
File srcFile = new File(scanner.next());
System.out.println("请输入线程个数:");
int n = scanner.nextInt();
if (!srcFile.exists()) {
System.out.println("该文件不存在!");
} File desFile = new File(srcFile.getParent(), "new" + srcFile.getName());
long partLenghth = srcFile.length() / n + 1;
for (int i = 1; i < n + 1; i++) {
       
// 每次传入单线程复制的长度
new MyThread(srcFile, desFile, partLenghth, MyThread.getPointer())
.start();
}
}
}

--> MyThread线程实现类

 package com.dragon.java.newthreadcopy;

 import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile; public class MyThread extends Thread {
private File srcFile;
private File desFile;
private long partLength;
private static long pointer = 0; MyThread() {
super();
} MyThread(File srcFile, File desFile, long partLength, long pointer) {
super();
this.srcFile = srcFile;
this.desFile = desFile;
this.partLength = partLength;
MyThread.pointer = pointer;
} @Override
public void run() {
RandomAccessFile rafSrc = null;
RandomAccessFile rafDes = null;
try {
rafSrc = new RandomAccessFile(srcFile, "r");
rafDes = new RandomAccessFile(desFile, "rw"); rafSrc.seek(pointer);
rafDes.seek(pointer);

         // 一次性复制完整的一部分长度
byte[] buffer = new byte[(int) partLength];
int len = rafSrc.read(buffer);
rafDes.write(buffer, 0, len);
pointer = rafSrc.getFilePointer();
} catch (IOException e) {
System.out.println(e);
}
} public static long getPointer() {
return pointer;
}
}

--> 感觉第二种也完全是多余的啊,就是一种方法...