zoukankan      html  css  js  c++  java
  • 基于直接缓冲区和非直接缓冲区的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));
  • 相关阅读:
    Message高级特性 & 内嵌Jetty实现文件服务器
    springboot中使用kindeditor富文本编辑器实现博客功能&vue-elementui使用vue-kindeditor
    Embarcadero RAD Studio XE5
    经典营销故事
    百度竞价教程 借助百度热力图让你的效果翻10倍
    无本借力:他是如何实现年收入70万?
    不用软件快速拥有几百个QQ群并都是管理员
    质保、保修、包修:含义不同
    域名反向解析在自建邮件群发服务器中的应用
    2014年1月1日,马年
  • 原文地址:https://www.cnblogs.com/Koalin/p/11198511.html
Copyright © 2011-2022 走看看