目前Java中最IO有多种文件读取的方法,本文章对比Stream,NIO ByteBuffer,NIO MappedByteBuffer的性能,让我们知道到底怎么能写出性能高的文件读取代码。

package com.seeyon.nio;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; /**
* Created by yangyu on 16/12/28.
*/ /**
* 比较Stream流,NIO ByteBuffer,NIO MappedByteBuffer性能对比
* 其中Stream最慢,NIO MappedByteBuffer最快
* Stream:1000ms
* NIO ByteBuffer:220ms
* NIO MappedByteBuffer:112ms
*/
public class Compare { /**
* 使用stream作为IO流读取和写入文件
* 速度:1000ms左右
*
* @throws IOException
*/
@Test
public void useStream() throws IOException { long startTime = System.currentTimeMillis();
/**
* 4000000个整数长度的文件
*/
int num = 2000 * 2000; /**
* 带缓冲的输出流,写文件
*/
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("/Users/yangyu/Downloads/compare.tmp")));
for (int i = 0; i < num; i++) {
dataOutputStream.writeInt(i);
}
dataOutputStream.close(); int data = 0;
/**
* 带缓冲的输入流,读文件
*/
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("/Users/yangyu/Downloads/compare.tmp")));
try {
while (true) {
data = in.readInt();
}
} catch (EOFException e) {
System.out.println("读取完成"+data);
}
in.close();
long endTime = System.currentTimeMillis();
System.out.println("ms:" + (endTime - startTime));
} /**
* 使用NIO ByteBuffer
* 时间:220ms
* @throws IOException
*/
@Test
public void useNioByteBuffer() throws IOException {
long startTime = System.currentTimeMillis();
int num = 2000*2000;
/**
* 文件输出流
*/
FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangyu/Downloads/compare.tmp");
/**
* NIO Channel 通道
*/
FileChannel fileChannel = fileOutputStream.getChannel();
/**
* ByteBuffer缓冲区
*/
ByteBuffer buffer = ByteBuffer.allocate(num*5);
for (int i = 0; i < num; i++) {
buffer.putInt(i);
}
/**
* 为写做准备
*/
buffer.flip();
/**
* 写操作
*/
fileChannel.write(buffer);
fileChannel.close(); /**
* 缓冲区
*/
ByteBuffer buffer1 = ByteBuffer.allocate(num*5);
/**
* 文件输入流
*/
FileInputStream in = new FileInputStream("/Users/yangyu/Downloads/compare.tmp");
/**
* 输入通道
*/
FileChannel fin = in.getChannel();
/**
* 为读取做准备
*/
buffer1.clear();
System.out.println(buffer1.limit());
/**
* 读取
*/
fin.read(buffer1);
fin.close(); long endTime = System.currentTimeMillis();
System.out.println("ms:" + (endTime - startTime));
buffer1.flip();
System.out.println(buffer1.limit());
} /**
* 使用MappedByteBuffer,通过FileChannel将文件映射到内存
* 时间:112ms
* @throws IOException
*/
@Test
public void useRandomAccess() throws IOException {
long startTime = System.currentTimeMillis();
int num = 2000*2000; /**
* 使用可随机访问位置的RandomAccessFile
*/
RandomAccessFile file = new RandomAccessFile("/Users/yangyu/Downloads/compare.tmp","rw");
/**
* 获取通道Channel
*/
FileChannel fileChannel = file.getChannel();
/**
* 将文件映射到缓冲区MappedByteBuffer
*/
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE,0,num*4);
/**
* 写文件
*/
for (int i = 0; i < num; i++) {
mappedByteBuffer.putInt(i);
}
fileChannel.close(); int data=0;
RandomAccessFile file1 = new RandomAccessFile("/Users/yangyu/Downloads/compare.tmp","rw");
FileChannel fc = file1.getChannel();
MappedByteBuffer mappedByteBuffer1 = fc.map(FileChannel.MapMode.READ_WRITE,0,file1.length());
/**
* 读文件
*/
while (mappedByteBuffer1.hasRemaining()){
data = mappedByteBuffer1.getInt();
}
fc.close();
long endTime = System.currentTimeMillis();
System.out.println("ms:" + (endTime - startTime));
System.out.println(data);
}
}

结论非常明显啦,以后再使用IO读写文件的时候,多使用NIO MappedByteBuffer吧,毕竟NIO比老IO性能好太多啦。

Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比的更多相关文章

  1. NIO与普通IO文件读写性能对比

    最近在熟悉java的nio功能.nio采用了缓冲区的方式进行文件的读写,这一点更接近于OS执行I/O的方式.写了个新旧I/O复制文件的代码,练练手,顺便验证一下两者读写性能的对比,nio是否真的比普通 ...

  2. java.nio.ByteBuffer中的flip()、rewind()、compact()等方法的使用和区别

    java.nio.ByteBuffer 1. ByteBuffer中的参数position.limit.capacity.mark含义: position:表示当前指针的位置(下一个要操作的数据元素的 ...

  3. 5种调优Java NIO和NIO.2的方式

    Java NIO(New Input/Output)——新的输入/输出API包——是2002年引入到J2SE 1.4里的.Java NIO的目标是提高Java平台上的I/O密集型任务的性能.过了十年, ...

  4. Java的BIO,NIO,AIO

    Java中的IO操作可谓常见.在Java的IO体系中,常有些名词容易让人困惑不解.为此,先通俗地介绍下这些名词. 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同 ...

  5. 顺序、随机IO和Java多种读写文件性能对比

    概述 对于磁盘的读写分为两种模式,顺序IO和随机IO. 随机IO存在一个寻址的过程,所以效率比较低.而顺序IO,相当于有一个物理索引,在读取的时候不需要寻找地址,效率很高. 基本流程 总体结构 我们编 ...

  6. Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer

    Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer 1. Java NIO(New Input/Output)1 1.1. 变更通知(因为每个事件都需要一个监听者 ...

  7. Java NIO、NIO.2学习笔记

    相关学习资料 http://www.molotang.com/articles/903.html http://www.ibm.com/developerworks/cn/education/java ...

  8. 【转载】Java NIO学习 & NIO BIO AIO 比较

    可以参考这个页面: http://www.iteye.com/magazines/132-Java-NIO (下面这个页面也有) http://ifeve.com/overview/ 另,在这篇文章里 ...

  9. java学习-NIO(五)NIO学习总结以及NIO新特性介绍

    我们知道是NIO是在2002年引入到J2SE 1.4里的,很多Java开发者比如我还是不知道怎么充分利用NIO,更少的人知道在Java SE 7里引入了更新的输入/输出 API(NIO.2).但是对于 ...

随机推荐

  1. 从Knockout到Angular的架构演变

    2008年第一次在WPF中使用MVVM模式之后,就一直热衷于耦合隔离.模块化与重构.UI和逻辑分离.单元测试以及后面的领域模型.谈及MVVM模式,自己也开发过一套框架,但没有长期更新和维护,所以索性就 ...

  2. CSharpGL(0)一个易学易用的C#版OpenGL

    +BIT祝威+悄悄在此留下版了个权的信说: CSharpGL(0)一个易学易用的C#版OpenGL CSharpGL是我受到SharpGL的启发,在整理了SharpGL,GLM,SharpFont等开 ...

  3. Azure SQL Database (21) 将整张表都迁移到Azure Stretch Database里

    <Windows Azure Platform 系列文章目录>  Azure SQL Database (19) Stretch Database 概览      Azure SQL Da ...

  4. Mongoose Schemas定义中timestamps选项的妙用

    在Node.js中使用MongoDB少不了Mongoose. 假设有如下Mongoose Schemas的定义: var ItemSchema = new mongoose.Schema({ biz: ...

  5. C语言 · 寻找数组中的最大值

    问题描述 对于给定整数数组a[],寻找其中最大值,并返回下标. 输入格式 整数数组a[],数组元素个数小于1等于100.输出数据分作两行:第一行只有一个数,表示数组元素个数:第二行为数组的各个元素. ...

  6. enum操作--获取枚举里的最大值

    一个应用系统,如果程序里没有任何enum的使用,我认为它的可读性是有待商榷的. 求枚举里的最大/最小枚举值, 其实是对Array进行操作: enum EnumTest { ddd = , eee } ...

  7. sublime3 插件

    Sublime Text 3能用支持的插件推荐 从二月份用测试版本build 3012开始用sublime text 3,虽然很多插件在sublime text 3不工作了,因为sublime tex ...

  8. iOS-C基础

    iOS开发系列--C语言之基础知识 概览 当前移动开发的趋势已经势不可挡,这个系列希望浅谈一下个人对IOS开发的一些见解,这个IOS系列计划从几个角度去说IOS开发: C语言 OC基础 IOS开发(i ...

  9. OLE DB Command transformation 用法

    OLE DB Command transformation component 能够引用参数,逐行调用sqlcommand,This transformation is typically used ...

  10. OPEN CASCADE BSpline Curve Interpolation

    OPEN CASCADE BSpline Curve Interpolation eryar@163.com Abstract. Global curve interpolation to point ...