Chinaunix首页 | 论坛 | 博客
  • 博客访问: 910026
  • 博文数量: 91
  • 博客积分: 803
  • 博客等级: 准尉
  • 技术积分: 1051
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 13:42
文章分类

全部博文(91)

文章存档

2021年(1)

2020年(4)

2019年(4)

2018年(9)

2017年(11)

2016年(11)

2015年(6)

2014年(3)

2013年(28)

2012年(14)

分类: Java

2018-05-07 21:34:53

多线程文件拷贝,具体使用了RandomAccessFile类,对源文件进行随机读,对目标文件进行随机写入。需注意需在线程内部去实例化RandomAccessFile类,进行操作避免资源句柄共享造成异常。

点击(此处)折叠或打开

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;

  4. public class FixedFile {

  5.     public static void main(String[] args) throws Exception {
  6.             File f = new File("a.txt");
  7.             long length = f.length();
  8.             System.out.println(length);
  9.             int threadNum = 3;
  10.             int split ;
  11.             if(length%3==0){
  12.                  split = (int) (length/3);
  13.             } else{
  14.                  split = (int) (length/3) + 1;
  15.             }
  16.             System.out.println(split);
  17.             
  18.             String src = "a.txt";
  19.             String dest = "b.txt";
  20.             //先建立目标文件
  21.             RandomAccessFile destFile = new RandomAccessFile(dest,"rw");
  22.             destFile.setLength(length);
  23.             destFile.close();
  24.             
  25.             for(int i =0; i<threadNum;i++ ) {
  26.                 new Thread(new MultiCopy(src,dest, i*split,split) ).start();
  27.             }
  28.     }

  29. }
  30. class MultiCopy implements Runnable{

  31.     private String from;
  32.     private String dest;
  33.     private int offset;
  34.     private int length;
  35.     public MultiCopy(String from,String dest,int offset, int length) {
  36.         this.from = from;
  37.         this.dest=dest;
  38.         this.offset=offset;
  39.         this.length=length;
  40.     }

  41.     @Override
  42.     public void run() {
  43.         RandomAccessFile srcFile = null;
  44.         RandomAccessFile destFile = null;
  45.         try {
  46.             srcFile = new RandomAccessFile(from,"r");
  47.             destFile = new RandomAccessFile(dest,"rw");
  48.             srcFile.seek(offset);
  49.             destFile.seek(offset);
  50.             byte [] buff = new byte[1024]; //缓冲区大小
  51.             int num = (length % 1024 ==0) ? (length / 1024) :(length / 1024 + 1) ; //迭代次数
  52.             for(int i=0;i<num;i++) {
  53.                 int perLength = srcFile.read(buff);
  54.                 if(perLength != -1) {
  55.                     destFile.write(buff);
  56.                 }
  57.             }
  58.         } catch (Exception e) {
  59.             e.printStackTrace();
  60.         } finally {
  61.             try {
  62.                 destFile.close();
  63.                 srcFile.close();
  64.             } catch (IOException e) {
  65.                 e.printStackTrace();
  66.             }
  67.         }
  68.     }
  69.     
  70. }

阅读(1581) | 评论(0) | 转发(0) |
0

上一篇:PHP导出CSV

下一篇:java 简单聊天室

给主人留下些什么吧!~~