• 基于直接缓冲区和非直接缓冲区的javaIO文件操作


    基本概念:

    1. 非直接缓冲区:  指的是通过jvm来缓存数据的,应用程序要读取本地数据要经历从本地磁盘到物理内存,然后copy到jvm中,然后再通过流的方式读取到应用程序中,写的操作正好与之相反。

    2. 直接缓冲区:指不通过应用程序读取磁盘的文件时不用经过jvm,而是直接由本地磁盘到物理内存,然后到应用程序。

    对比:直接缓冲方式会比非直接缓冲方式快,不过在保存文件到本地过程中,文件先保存到物理内存,途中如果用清理内存工具来进行清理的话,数据就会丢失。因此非直接缓冲区的方式比较安全。

    我们来看一下直接缓冲IO操作的代码:

    long statTime=System.currentTimeMillis();
    		//创建管道
    		FileChannel   inChannel=	FileChannel.open(Paths.get("D://test001.mp4"), StandardOpenOption.READ);
    		FileChannel   outChannel=	FileChannel.open(Paths.get("D://test002.mp4"), StandardOpenOption.READ,StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    	    //定义映射文件
    		MappedByteBuffer inMappedByte = inChannel.map(MapMode.READ_ONLY,0, inChannel.size());
    		MappedByteBuffer outMappedByte = outChannel.map(MapMode.READ_WRITE,0, inChannel.size());
    		//直接对缓冲区操作
    		byte[] dsf=new byte[inMappedByte.limit()];
    		inMappedByte.get(dsf);
    		outMappedByte.put(dsf);
    		inChannel.close();
    		outChannel.close();
    		long endTime=System.currentTimeMillis();
    		System.out.println("操作直接缓冲区耗时时间:"+(endTime-statTime));
    

      再来对比一下非直接缓冲区IO操作的代码:

                    long statTime=System.currentTimeMillis();
            // 读入流
            FileInputStream fst = new FileInputStream("D://test001.mp4");
            // 写入流
            FileOutputStream fos = new FileOutputStream("D://test002.mp4");
            // 创建通道
            FileChannel inChannel = fst.getChannel();
            FileChannel outChannel = fos.getChannel();
            // 分配指定大小缓冲区
            ByteBuffer buf = ByteBuffer.allocate(1024);
            while (inChannel.read(buf) != -1) {
                // 开启读取模式
                buf.flip();
                // 将数据写入到通道中
                outChannel.write(buf);
                buf.clear();
            }
            // 关闭通道 、关闭连接
            inChannel.close();
            outChannel.close();
            fos.close();
            fst.close();
            long endTime=System.currentTimeMillis();
            System.out.println("操作非直接缓冲区耗时时间:"+(endTime-statTime));
  • 相关阅读:
    CentOS 7.0安装Zimbra 8.6邮件服务器
    centos7备份还原与grub2引导和rescue模式修改root密码
    通过grub硬盘安装centos7
    CentOS系统中常用查看系统信息和日志命令小结
    安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(一)
    linux 下shell脚本执行多个命令的方法
    centos Crontab
    抓取某网站信息时遇到的问题及解决 The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set
    HttpClient不必每次新建实例而RestSharp推荐新建实例的原因
    .net core读取json配置文件
  • 原文地址:https://www.cnblogs.com/hglSV/p/11198511.html
Copyright © 2020-2023  润新知