JAVA NIO(三) 三种文件的复制方法与效率对比

文件的复制最能体现io效率了.因为既需要读取数据还需要写出到硬盘中,下面提供了三种文件复制的方法 可以对比一下 直接缓冲区与非直接缓冲区的效率对比.




public class Nio {

    public static void main(String[] args) throws IOException {
        nioCopyTest1();
        nioCopyTest2();
        nioCopyTest3();
    }

    /**通道之间的数据传输(直接缓冲区的模式)
     *
     */

    private static void   nioCopyTest3() throws IOException {
        long startTime = System.currentTimeMillis();

        FileChannel inChannel = FileChannel.open(Paths.get("E:\\ 1.avi"), StandardOpenOption.READ);

        FileChannel outChennel = FileChannel.open(Paths.get("E:\\ 13.avi"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        outChennel.transferFrom(inChannel,0,inChannel.size());

        long end = System.currentTimeMillis();
        System.out.println("nioCopyTest3耗费时间:"+(end-startTime));
    }

    /**
     * 使用直接缓冲区完成文件的复制(内存映射文件)
     */
    private static void  nioCopyTest2() throws IOException {
        long startTime = System.currentTimeMillis();

        FileChannel inChannel = FileChannel.open(Paths.get("E:\\ 1.avi"), StandardOpenOption.READ);

        FileChannel outChennel = FileChannel.open(Paths.get("E:\\ 12.avi"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        //内存映射文件(什么模式 从哪开始 到哪结束)
        MappedByteBuffer inMappeBuf = inChannel.map(FileChannel.MapMode.READ_ONLY,0,inChannel.size());
        MappedByteBuffer outMappeBuf =  outChennel.map(FileChannel.MapMode.READ_WRITE,0,inChannel.size());

        //直接都缓冲区进行数据的读写操作

        byte[] dst = new byte[inMappeBuf.limit()];
        inMappeBuf.get(dst);
        outMappeBuf.put(dst);

        inChannel.close();
        outChennel.close();
        long end = System.currentTimeMillis();
        System.out.println("nioCopyTest2耗费时间:"+(end-startTime));
    }


    /**
     *  非直接缓冲区 文件的复制
     * @throws IOException
     */
    private static void nioCopyTest1()throws IOException {
        long startTime = System.currentTimeMillis();
        FileInputStream fileInputStream = new FileInputStream(new File("E:\\ 1.avi"));
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\ 11.avi");

        //获取通道
        FileChannel inChannel = fileInputStream.getChannel();
        FileChannel outChanel = fileOutputStream.getChannel();

        //分配缓冲区的大小
        ByteBuffer buf = ByteBuffer.allocate(1024);

        //将通道中的数据存入缓冲区
        while (inChannel.read(buf) != -1) {
            buf.flip();//切换读取数据的模式
            outChanel.write(buf);
            buf.clear();
        }
        outChanel.close();
        inChannel.close();
        fileInputStream.close();
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("nioCopyTest1耗费时间:"+(end-startTime));
    }
}


控制台输出:


nioCopyTest1耗费时间:1417
nioCopyTest2耗费时间:142

nioCopyTest3耗费时间:68


直接缓冲不需要文件的复制拷贝 所以大大增加效率 其中第三种方法通道之间的文件传输 速度最快了直接在通道中完成.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值