1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下:

以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区。参看read(byte b[])方法的源码,可知,它会在内部再调用readBytes(b, 0, b.length)方法,而且readBytes(b, 0, b.length)方法是一个native方法(即本地方法),最终通过这个本地方法来发起一次系统调用,即调用系统内核的read()方法,内核从磁盘读取数据到内核缓冲区,这个过程由磁盘控制器通过DMA操作将数据从磁盘读取取内核缓冲区,此过程不依赖于CPU。然后用户进程再将数据从内核缓冲区拷贝到用户空间缓冲区。用户进程再从用户空间缓冲区中读取数据。因为用户进程是不可以直接访问硬件的。所以需要通过内核来充当中间人的作用来实现文件的读取。整个过程如下图所示:

2.  自从JAVA 1.4以后,JAVA在NIO在引入了文件通道的概念,在API中有提供了一个FileChannel类。该类与传统的IO流进行关联。可以由FileInputStream或FileOutputStream获取该文件通道,我们可以通过通道对文件进行读写操作。

3.JAVA NIO中还引入了文件内存映射的概念:现代操作系统大都支持虚拟内存映射,这样,我们可以把内核空间地址与用户空间的虚拟地址映射到同一个物理地址,这样,DMA 硬件(只能访问物理内存地址)就可以填充对内核与用户空间进程同时可见的缓冲区了。如下图所示:

这样做的好处是,我们在读取磁盘文件时,再也不用通过内核缓冲区到用户进程缓冲区的来回拷贝操作了。操作系统会通过一些页面调度算法来将磁盘文件载入对分页区进行高速缓存的物理内存。我们就可以通过映射后物理内存来读取磁盘文件了。

4.  下面我们通过三种不同方式文件拷贝的案例来验证文件通道及文件内存映射在IO系统中的作用。测试环境为windows 32位系统和JDK1.6。代码中使用的测试文件movie.avi为一个123MB的视频文件。代码如下:

package cn.com.hbust.nio.file; 

import java.io.File; 

import java.io.FileInputStream; 

import java.io.FileOutputStream; 

import java.nio.MappedByteBuffer; 

import java.nio.channels.FileChannel; 

public class FileCopyTest { 

    public  static  void main(String[] args) throws Exception { 

       String sourcePath = "F:\\mywork\\javademo\\dir1\\movie.avi"; 

       String destPath1 = "F:\\mywork\\javademo\\dir2\\movie1.avi"; 

       String destPath2 = "F:\\mywork\\javademo\\dir2\\movie2.avi"; 

       String destPath3 = "F:\\mywork\\javademo\\dir2\\movie3.avi"; 

       long t1 = System.currentTimeMillis(); 

       traditionalCopy(sourcePath,destPath1); 

       long t2 = System.currentTimeMillis(); 

       System.out.println("传统IO方法实现文件拷贝耗时:" + (t2-t1) + "ms"); 

       nioCopy(sourcePath,destPath2); 

       long t3 = System.currentTimeMillis(); 

       System.out.println("利用NIO文件通道方法实现文件拷贝耗时:" + (t3-t2) + "ms"); 

       nioCopy2(sourcePath,destPath3); 

       long t4 = System.currentTimeMillis(); 

       System.out.println("利用NIO文件内存映射及文件通道实现文件拷贝耗时:" + (t4-t3) + "ms"); 

    } 

    private  static  void nioCopy2(String sourcePath, String destPath) throws Exception { 

       File source = new File(sourcePath); 

       File dest = new File(destPath); 

       if(!dest.exists()) { 

           dest.createNewFile(); 

       } 

       FileInputStream fis = new FileInputStream(source); 

       FileOutputStream fos = new FileOutputStream(dest); 

       FileChannel sourceCh = fis.getChannel(); 

       FileChannel destCh = fos.getChannel(); 

       MappedByteBuffer mbb = sourceCh.map(FileChannel.MapMode.READ_ONLY, 0, sourceCh.size()); 

       destCh.write(mbb); 

       sourceCh.close(); 

       destCh.close(); 

    } 

    private  static  void traditionalCopy(String sourcePath, String destPath) throws Exception{ 

       File source = new File(sourcePath); 

       File dest = new File(destPath); 

       if(!dest.exists()) { 

           dest.createNewFile(); 

       } 

       FileInputStream fis = new FileInputStream(source); 

       FileOutputStream fos = new FileOutputStream(dest); 

       byte [] buf = newbyte [512]; 

       int len = 0; 

       while((len = fis.read(buf)) != -1) { 

           fos.write(buf, 0, len); 

       } 

       fis.close(); 

       fos.close(); 

    } 

    private  static  void nioCopy(String sourcePath, String destPath) throws Exception{ 

       File source = new File(sourcePath); 

       File dest = new File(destPath); 

       if(!dest.exists()) { 

           dest.createNewFile(); 

       } 

       FileInputStream fis = new FileInputStream(source); 

       FileOutputStream fos = new FileOutputStream(dest); 

       FileChannel sourceCh = fis.getChannel(); 

       FileChannel destCh = fos.getChannel(); 

       destCh.transferFrom(sourceCh, 0, sourceCh.size()); 

       sourceCh.close(); 

       destCh.close(); 

    } 

}

  

每执行完一次拷贝之后,将F:\mywork\javademo\dir2\目录中的内容删除掉,重复执行8次。观察测试结果如下:时间单位为ms(毫秒)

由上表可知,传统IO方式平均拷贝完成时间约为1968ms,NIO文件通道方式平均拷贝完成时间约为1672ms,文件内存映射及文件通道方式平均拷贝完成时间约为1418ms。

转载自http://www.open-open.com/lib/view/open1413518521372.html

Java IO和Java NIO 和通道 在文件拷贝上的性能差异分析的更多相关文章

  1. Java IO和Java NIO在文件拷贝上的性能差异分析

    1.  在JAVA传统的IO系统中,读取磁盘文件数据的过程如下: 以FileInputStream类为例,该类有一个read(byte b[])方法,byte b[]是我们要存储读取到用户空间的缓冲区 ...

  2. hive运行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:

    hive> select product_id, track_time from trackinfo limit 5; Total MapReduce jobs = 1 Launching Jo ...

  3. java.io.OutputStream & java.io.FileOutputStream

    java.io.OutputStream & java.io.FileOutputStream 1.Java.io.OutputStream(字节输出流) 字节输出流,这是一个抽象类,是表示输 ...

  4. hadoop报错:java.io.IOException(java.net.ConnectException: Call From xxx/xxx to xxx:10020 failed on connection exception: java.net.ConnectException: 拒绝连接

    任务一直报错 现象比较奇怪,部分任务可以正常跑,部分问题报错 报错信息如下: Ended Job = job_1527476268558_132947 with exception 'java.io. ...

  5. java.io.IOException: java.sql.SQLException: ORA-01502: index 'BTO.PK_xxxxx' or partition of such index is in unusable state

    最近由于数据库的全备出问题了,所以一直在观察. 刚好发现很多不需要的数据,就删了几百个G的数据吧. 今天突然就报这个问题. java.io.IOException: java.sql.SQLExcep ...

  6. ERROR tool.ImportTool: Import failed: java.io.IOException: java.lang.ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf

    sqoop从mysql导入到hive报错: 18/08/22 13:30:53 ERROR tool.ImportTool: Import failed: java.io.IOException: j ...

  7. sqoop mysql--->hive 报错 (ERROR tool.ImportTool: Import failed: java.io.IOException: java.lang.ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf)

    ERROR tool.ImportTool: Import failed: java.io.IOException: java.lang.ClassNotFoundException: org.apa ...

  8. Hive报错 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name%7D

    报错信息如下 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyn ...

  9. Intellij IDEA报错:Could not save application settings: java.io.IOException: java.lang.AssertionError: Unexpected content storage modificat

    Question: i have a message saying "Could not save application settings: java.io.IOException: ja ...

随机推荐

  1. python 进程间通信(上)

    一  使用queue来实现进程间的内存共享 #_*_coding:utf-8_*_ from multiprocessing import Process,Queue import os,time d ...

  2. 03-Python入门学习-Python基础

    一.与用户交互 1.什么是与用户交互 程序等待用户输入一些数据,然后程序执行完毕后为用户反馈信息 2.为何程序要与用户交互 为了让计算机能够像人一样与用户的交互 3.如何用 在python3中:inp ...

  3. webpack实现开发、测试、生产等环境的打包切换

    使用webpack构建的工程,在开发过程中不同环境的配置不同,在各种环境的打包切换过程中需要手动修改相关配置达到预期目的.但是每次都手动修改会比较麻烦,本文简单介绍如何通过对webpack进行配置,实 ...

  4. angular.isElement()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Oracle 11g修改字符集

    选择静默安装的安装字符集为默认的ZHS16GBK,工作中字符集为为AL32UTF8 一.登录oracle sqlplus / as sysdba shutdown immediate; STARTUP ...

  6. linux下fcitx的安装与配置

    首先安装fcitx pacman -S fcitx-im fcitx-config fcitx-cloudpinyin 之后进行配置 nano ~/.xprofile 写入 export XIM=fc ...

  7. EF Core HasQueryFilter 的小坑

    这是今天在实际项目中遇到的一个问题,Entity Framework Core 2.2 生成了下面的 SQL 语句,INNER JOIN 部分丑陋的 SQL 语句让人无法忍受. SELECT TOP( ...

  8. SaaS的先鋒:多合一讯息处理器

    在Rocketbots,我们的使命是改善企业与客户之间的关係.全球有超过40亿个消息传递帐户,人们花在上线对话的时间比任何其他活动都多.这种转变,创造了一个更高效及贴身的沟通.随著消费者越来越多要求, ...

  9. 封装和static 以及关键字“this”的用法

    1 封装 1.1 构造方法 构造方法是一种特殊的方法,专门用于构造/实例化对象,形式 [修饰符] 类名(){}: 1.1.1 无参构造 无参构造方法就是构造方法没有任何参数.构造方法在创建对象(new ...

  10. 软件工程第二次作业-VSTS单元测试

    一.选择开发工具 开发工具选择 Visual studio 2017 社区版,开发语言为C 由于之前已经安装完毕,所以不上传安装过程,主界面如下: 二.练习自动单元测试 使用的测试工具是VSTS,具体 ...