jAVA 提高文件复制性能之多线程复制文件

利用IO流中的随机访问文件 RandomAccessFile 和文件通道 FileChanne 复制文件可大大提高文件的读写效率,在此基础上利用多线程复制文件使其性能更优。因线程的个数可根据文件的大小及需求而定。其大概原理是根据给定的线程个数,将文件分段,每个线程负责的数据大小=文件长度/线程个数,将不能除尽的部分留给最后一段文件所分配的线程处理。以下是实现代码及自己理解的注释,多有偏差请见谅。下面是兄弟连java培训

总结的一些代码:供参考。

  程序实现类代码:

  importjava.io.RandomAccessFile;

  importjava.nio.channels.FileChannel;

  importjava.nio.channels.FileLock;

  //定义一个CopyThread类继承Thread类

  public class CopyThreadextends Thread{

  private String srcPath;//原文件地址

  private String destPath;//目标文件地址

  private int start,end;//start指定起始位置,end指定结束位置

  //构造CopyThread方法

  public CopyThread(StringsrcPath, String destPath, int start, int end) {

  this.srcPath = srcPath;//要复制的源文件路径

  this.destPath = destPath;//复制到的文件路径

  this.start = start;//复制起始位置

  this.end = end;//复制结束位置

  }

  //重写run()方法

  public void run() {

  try {

  //创建一个只读的随机访问文件

  RandomAccessFile in = newRandomAccessFile(srcPath, "r");

  //创建一个可读可写的随机访问文件

  RandomAccessFile out = newRandomAccessFile(destPath, "rw");

  in.seek(start);// 将输入跳转到指定位置

  out.seek(start);// 从指定位置开始写

  FileChannel inChannel =in.getChannel(); //文件输入通道

  FileChannel outChannel =out.getChannel();//文件输出通道

  //锁住需要操作的区域,false代表锁住

  FileLock lock = outChannel.lock(start,(end-start), false);

  //将字节从此通道的文件传输到给定的可写入字节的outChannel通道。

  inChannel.transferTo(start,(end-start), outChannel);

  lock.release();//释放锁

  out.close();//从里到外关闭文件

  in.close();//关闭文件

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  测试类代码:

  import java.io.File;

  public class TestMain {

  public static voidmain(String[] args) {

  //要复制的源文件路径

  String srcPath ="F:\\sun\\上课笔记\\aa.txt";

  String destPath ="F:\\sun\\上课笔记\\aa复件.txt";

  // 获得源文件长度

  File f = new File(srcPath);

  long len = f.length();

  int count = 3;// 需要的线程数

  int oneNum = (int) (len /count);//每个线程负责的文件长度,强制转换成int类型

  //用for循环处理划分文件的第一部分跟第二部分(循环次数可根据定义的线程数调整)

  for (int i = 0; i <count - 1; i++) {

  //oneNum * i 起始位置, oneNum * (i + 1)要复制数据的长度

  CopyThread ct = newCopyThread(srcPath, destPath, oneNum * i,oneNum * (i + 1));

  ct.start();

  }

  //文件长度不能整除的部分放到最后一段处理

  CopyThread ct = newCopyThread(srcPath, destPath, oneNum * (count-1),(int)len);

  ct.start();

  }

  }

猜你喜欢

转载自blog.csdn.net/qq_36318234/article/details/79899938