分四个进程拷贝一个大文件

public class ThreadFileCopy extends Thread{

    private File source;
    private File target;
    long start;
    long end;


    public ThreadFileCopy(File source, File target, long start, long end) {
        super();
        this.source = source;
        this.target = target;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        System.out.println(this.getName()+"开始拷贝");

        int count = 0;
        RandomAccessFile input = null;
        RandomAccessFile output = null;

        try {
            input = new RandomAccessFile(source, "r");
            output = new RandomAccessFile(target, "rw");
            input.seek(start);
            output.seek(start);

            byte[] b = new byte[1024];
            int len = 0;
            while(((len = input.read(b)) != -1 )&& (count <= end - start))
            {
                count += len;
                output.write(b, 0, len);
            }
            System.out.println(this.getName()+"拷贝结束"+count);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO: handle exception
        }finally {
            try {
                if(output!=null)output.close();
                if(input!=null)input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {

        File source = new File("C:\\Users\\lx\\Desktop\\larva搞笑虫子.rar");
        File target = new File("C:\\Users\\lx\\Desktop\\新建文件夹",source.getName());

        long item = source.length()/4;
        for(int i = 0;i<4;i++)
        {
            new ThreadFileCopy(source, target, i*item, (i+1)*item).start();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值