多个线程对象,复制同一个文件

利用多个线程可实现对文件的复制效率更高(小文件复制)

public class MyTest2 {

    public static void main(String[] args) throws IOException {

        new CopyThread(0).start();
        new CopyThread(1024*1024*2).start();
        new CopyThread(1024*1024*4).start();
        new CopyThread(1024*1024*6).start();
        new CopyThread(1024*1024*8).start();
    }

}

class CopyThread extends Thread{
    int filepointer;
    public CopyThread(int filepointer) {
        this.filepointer=filepointer;
    }

    @Override
    public void run() {
        try {

            File sourcefile = new File("C:\\Users\\Administrator\\Desktop\\test\\许巍-歌曲大联唱.mp3");
            File targetfile = new File("C:\\Users\\Administrator\\Desktop\\test\\许巍-歌曲大联唱copy.mp3");
            CopyFile(filepointer, sourcefile, targetfile);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void CopyFile(long filepointer, File sourcefile, File targetfile) throws IOException {

            RandomAccessFile read= new RandomAccessFile(sourcefile,"rw");
            RandomAccessFile write = new RandomAccessFile(targetfile, "rw");
            int len=0;
            byte[] bytes = new byte[0];
            bytes=new byte[1024*1024*2];
            read.seek(filepointer);
            write.seek(filepointer);
            len=read.read(bytes);
            write.write(bytes,0,len);

            filepointer = read.getFilePointer();
            System.out.println(filepointer);
            read.close();
            write.close();

    }
}

采用多个线程对象复制大文件

  1. 大文件按照占用空间大小量级创建线程数,比如3.83G,可以创建三个线程,用来复制前三个G;
  2. 最后的部分再创建新的线程复制即可,不可以放在循环中复制,那样就会复制出4个G的文件副本;
  3. 子线程类中需要加入判断,即复制的开始指针总是小于终止指针,即start<end,这样才能保证复制完全,不会出现负寻址异常;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class MyTest2 {
    public static void main(String[] args) throws IOException {
       
        //
        File sourcefile = new File("G:\\64WIN7.GHO");
        File targetfile = new File("C:\\Users\\Administrator\\Desktop\\test\\64WIN7.GHO");
        long length = sourcefile.length();
        System.out.println(length);
        //创建三个线程复制大文件;
        long threadNum=3;
        long singlecopy = length / 3;
        for (long i = 0; i < threadNum; i++) {
            long start=singlecopy*i;
            long end=(singlecopy*(i+1))-1;
            System.out.println(end);
            
            new CopyThread(start,end,sourcefile,targetfile).start();
        }
        //创建新的线程,复制剩下的数据
        long yu = length % threadNum;
        if (yu!=0){
            long start=singlecopy*threadNum;
            long end=length;
            System.out.println(end);
            new CopyThread(start,end,sourcefile,targetfile).start();
        }
    }
}

class CopyThread extends Thread{
    long start;
    long end;
    RandomAccessFile read;
    RandomAccessFile write;

    public CopyThread(long start, long end, File sourcefile, File targetfile) throws FileNotFoundException {
        this.start=start;
        this.end=end;
        read= new RandomAccessFile(sourcefile,"rw");
        write = new RandomAccessFile(targetfile, "rw");
    }

    @Override
    public void run() {
        try {
            read.seek(start);
            write.seek(start);
            int len=0;
            byte[] bytes=new byte[1024*1024];
            while ((start < end)&&(len=read.read(bytes))!=-1){
                start+=len;
                write.write(bytes,0,len);
            }
            read.close();
            write.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值