美文网首页
OIO、NIO复制文件的比较

OIO、NIO复制文件的比较

作者: CXY_XZL | 来源:发表于2021-09-17 17:17 被阅读0次

1.OIO(BIO)复制文件代码

public static void blockCopyFile(){

        //复制文件39.4G ,共计时间 171s
        InputStream input = null;
        OutputStream output = null;
        File srcFile = new File("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip");
        File destFile = new File("C:\\Users\\zxiong\\Downloads\\gradle-7.0_9.zip");

        try {
            //如果目标文件不存在,则新建
            if (!destFile.exists()) {
                destFile.createNewFile();
            }

            long startTime = System.currentTimeMillis();

            input = new FileInputStream(srcFile);
            output = new FileOutputStream(destFile);
            byte[] buf = new byte[1024*1024*100];
            int bytesRead;
            while ((bytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, bytesRead);
            }
            output.flush();
            long endTime = System.currentTimeMillis();
            log.debug("IO流复制秒数:" + (endTime - startTime)/1000);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(input);
            IoUtil.close(output);
        }
    }

2.NIO复制文件代码一

public static void test5() throws Exception{

        //400M/S  149s  39.4G
        FileChannel in4 = new FileInputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip").getChannel();
        FileChannel out4 = new FileOutputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_5.zip").getChannel();
        long size = in4.size();
        ByteBuffer bb = ByteBuffer.allocate(1024*1024);
        int length;
        long s = 0;
        long w = 0;
        Instant start = Instant.now();
        NumberFormat numberFormat = NumberFormat.getInstance();
        while((length = in4.read(bb)) != -1){
            s += length;
            String sFormat = numberFormat.format((float) s / (float) size * 100);
            log.info("文件读取进度:{}%", sFormat);
            for (int j = 0; j <= String.valueOf(sFormat).length(); j++) {
                System.out.print("\b");
            }
            bb.flip();
            int len = 0;
            while ((len = out4.write(bb)) != 0){
                w += len;
                String wFormat = numberFormat.format((float) w / (float) size * 100);
                log.info("文件写入进度:{}%", wFormat);
                for (int j = 0; j <= String.valueOf(wFormat).length(); j++) {
                    System.out.print("\b");
                }
            }
            bb.clear();
        }
        in4.close();
        out4.close();
        Instant end = Instant.now();
        long seconds = Duration.between(start, end).getSeconds();
        log.info("文件第五次复制用时(秒):{}",seconds);
    }

3.NIO复制文件代码二

public static void test6() throws Exception{

        //250M/S  161s  39.4G
        FileChannel in4 = new FileInputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip").getChannel();
        FileChannel out4 = new FileOutputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_5.zip").getChannel();
        long size = in4.size();
        ByteBuffer bb = ByteBuffer.allocate(1024*1024*512);
        int length;
        long s = 0;
        long w = 0;
        Instant start = Instant.now();
        NumberFormat numberFormat = NumberFormat.getInstance();
        while((length = in4.read(bb)) != -1){
            s += length;
            String sFormat = numberFormat.format((float) s / (float) size * 100);
            log.info("文件读取进度:{}%", sFormat);
            for (int j = 0; j <= String.valueOf(sFormat).length(); j++) {
                System.out.print("\b");
            }
            bb.flip();
            int len = 0;
            while ((len = out4.write(bb)) != 0){
                w += len;
                String wFormat = numberFormat.format((float) w / (float) size * 100);
                log.info("文件写入进度:{}%", wFormat);
                for (int j = 0; j <= String.valueOf(wFormat).length(); j++) {
                    System.out.print("\b");
                }
            }
            bb.clear();
        }
        in4.close();
        out4.close();
        Instant end = Instant.now();
        long seconds = Duration.between(start, end).getSeconds();
        log.info("文件第六次复制用时(秒):{}",seconds);
    }

3.NIO复制文件代码三

 public static void test1() throws Exception{
        //39.4G 140s 
        Instant start = Instant.now();
        FileChannel in1 = new FileInputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_2.zip").getChannel();
        FileChannel out1 = new FileOutputStream("C:\\Users\\zxiong\\Downloads\\gradle-7.0_1.zip").getChannel();
        try {
            long size = in1.size();
            log.info("size:{}",size);
            out1.transferFrom(in1, 0, size);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Instant end = Instant.now();
        long seconds = Duration.between(start, end).getSeconds();
        log.info("文件第一次复制时间(秒):{}",seconds);
    }

4.当前测试环境

interl i7 4核 内存8G

5.总结

在大文件传输上,NIO比BIO传输速率更快一些,以上示例还没有运用到多线程传递数据

相关文章

  • OIO、NIO复制文件的比较

    1.OIO(BIO)复制文件代码 2.NIO复制文件代码一 3.NIO复制文件代码二 3.NIO复制文件代码三 4...

  • NIO服务

    本文开发语言基于Java 代码参考SocketServer 目录 OIOSyncAsync NIO 小结 OIO ...

  • 2.netty传输

    Netty传输: NIO:非阻塞传输OIO:阻塞传输Local:JVM内部传输Embedded:测试Channel...

  • java nio复制文件

  • Java的nio读书笔记

    本文的分析基于java8,jdk的版本为jdk1.8.0_91。 什么是nio 概念 与oio的对比 典型代码 以...

  • Java IO 学习(一)同步/异步/阻塞/非阻塞

    关于IO,同步/异步/阻塞/非阻塞,这几个关键词是经常听到的,譬如:“Java oio是阻塞的,nio是非阻塞的”...

  • 从I/O模型到Netty(三)

    零、写在前面 本文虽然是讲Netty,但实际更关注的是Netty中的NIO的实现,所以对于Netty中的OIO(O...

  • 使用Java NIO完成文件上传

    使用NIO完成文件上传处理 参考文章 关于NIO文件操作的文章可以参阅:nio操作文件 代码 总结 可见,使用NI...

  • Java NIO FileChannel

    Java NIO FileChannel Java NIO FileChannel是连接到文件的通道。 使用文件通...

  • netty启动流程分析1

    在编写一个基于netty的网络应用中,代码的整体结构都一样,不管是写一个基于nio的还是Oio的netty应用,但...

网友评论

      本文标题:OIO、NIO复制文件的比较

      本文链接:https://www.haomeiwen.com/subject/nzcsgltx.html