java-IO操作性能对照
在软件系统中。IO速度比内存速度慢,IO读写在非常多情况下会是系统的瓶颈。
在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作。以字节为处理单位;Reader和Writer实现了Buffered缓存,以字符为处理单位。
从Java1.4開始,添加NIO(New IO),添加缓存Buffer和通道Channel,以块为处理单位。是双向通道(可读可写。类似RandomAccessFile),支持锁和内存映射文件訪问接口。大大提升了IO速度。
下面样例简单測试常见IO操作的性能速度。
/**
* 測试不同io操作速度
*
* @author peter_wang
* @create-time 2014-6-4 下午12:52:48
*/
public class SpeedTest {
private static final String INPUT_FILE_PATH = "io_speed.txt";
private static final String OUTPUT_FILE_PATH = "io_speed_copy.txt"; /**
* @param args
*/
public static void main(String[] args) {
long ioStreamTime1 = ioStreamCopy();
System.out.println("io stream copy:" + ioStreamTime1); long ioStreamTime2 = bufferedStreamCopy();
System.out.println("buffered stream copy:" + ioStreamTime2); long ioStreamTime3 = nioStreamCopy();
System.out.println("nio stream copy:" + ioStreamTime3); long ioStreamTime4 = nioMemoryStreamCopy();
System.out.println("nio memory stream copy:" + ioStreamTime4);
} /**
* 普通文件流读写
*
* @return 操作的时间
*/
private static long ioStreamCopy() {
long costTime = -1;
FileInputStream is = null;
FileOutputStream os = null;
try {
long startTime = System.currentTimeMillis();
is = new FileInputStream(INPUT_FILE_PATH);
os = new FileOutputStream(OUTPUT_FILE_PATH);
int read = is.read();
while (read != -1) {
os.write(read);
read = is.read();
}
long endTime = System.currentTimeMillis();
costTime = endTime - startTime;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return costTime;
} /**
* 增加缓存的文件流读写, Reader默认实现缓存。仅仅能读取字符文件,无法准确读取字节文件如图片视频等
*
* @return 操作的时间
*/
private static long bufferedStreamCopy() {
long costTime = -1;
FileReader reader = null;
FileWriter writer = null;
try {
long startTime = System.currentTimeMillis();
reader = new FileReader(INPUT_FILE_PATH);
writer = new FileWriter(OUTPUT_FILE_PATH);
int read = -1;
while ((read = reader.read()) != -1) {
writer.write(read);
}
writer.flush();
long endTime = System.currentTimeMillis();
costTime = endTime - startTime;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return costTime;
} /**
* nio操作数据流
*
* @return 操作的时间
*/
private static long nioStreamCopy() {
long costTime = -1;
FileInputStream is = null;
FileOutputStream os = null;
FileChannel fi = null;
FileChannel fo = null;
try {
long startTime = System.currentTimeMillis();
is = new FileInputStream(INPUT_FILE_PATH);
os = new FileOutputStream(OUTPUT_FILE_PATH);
fi = is.getChannel();
fo = os.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true) {
buffer.clear();
int read = fi.read(buffer);
if (read == -1) {
break;
}
buffer.flip();
fo.write(buffer);
}
long endTime = System.currentTimeMillis();
costTime = endTime - startTime;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fi != null) {
fi.close();
}
if (fo != null) {
fo.close();
}
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return costTime;
} /**
* nio内存映射操作数据流
*
* @return 操作的时间
*/
private static long nioMemoryStreamCopy() {
long costTime = -1;
FileInputStream is = null;
//映射文件输出必须用RandomAccessFile
RandomAccessFile os = null;
FileChannel fi = null;
FileChannel fo = null;
try {
long startTime = System.currentTimeMillis();
is = new FileInputStream(INPUT_FILE_PATH);
os = new RandomAccessFile(OUTPUT_FILE_PATH, "rw");
fi = is.getChannel();
fo = os.getChannel();
IntBuffer iIb=fi.map(FileChannel.MapMode.READ_ONLY, 0, fi.size()).asIntBuffer();
IntBuffer oIb = fo.map(FileChannel.MapMode.READ_WRITE, 0, fo.size()).asIntBuffer();
while(iIb.hasRemaining()){
int read = iIb.get();
oIb.put(read);
}
long endTime = System.currentTimeMillis();
costTime = endTime - startTime;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fi != null) {
fi.close();
}
if (fo != null) {
fo.close();
}
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return costTime;
} }
执行结果:
io stream copy:384
buffered stream copy:125
nio stream copy:12
nio memory stream copy:10
结论分析:
最普通的InputStream操作耗时较长。添加了缓存后速度添加了,用了nio和内存映射訪问文件。速度最快。
java-IO操作性能对照的更多相关文章
- Java IO设计模式彻底分析 (转载)
一.引子(概括地介绍Java的IO) 无论是哪种编程语言,输入跟输出都是重要的一部分,Java也不例外,而且Java将输入/输出的功能和使用范畴做了很大的扩充.它采用了流的 机制来实现输入/输出,所谓 ...
- Java IO(Properties/对象序列化/打印流/commons-io)
Java IO(Properties/对象序列化/打印流/commons-io) Properties Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载. ...
- Java IO之简单输入输出
Java中的IO分为两个部分,以InputStream和Reader为基类的输入类,以OutputStream和Writer为基类的输出类. 当中InputStream和OutputStream以字节 ...
- java IO流体系--通识篇
1.I/O流是什么 Java的I/O流是实现编程语言的输入/输出的基础能力,操作的对象有外部设备.存储设备.网络连接等等,是所有服务器端的编程语言都应该具备的基础能力. I = Input(输入),输 ...
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- Java IO之字符流和文件
前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
- java Io流向指定文件输入内容
package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...
- java Io文件输入输出流 复制文件
package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...
- java Io流更新文件内容
package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...
随机推荐
- Thinkphp入门 二 —空操作、空模块、模块分组、前置操作、后置操作、跨模块调用(46)
原文:Thinkphp入门 二 -空操作.空模块.模块分组.前置操作.后置操作.跨模块调用(46) [空操作处理] 看下列图: 实际情况:我们的User控制器没有hello()这个方法 一个对象去访问 ...
- 谈谈Ext JS的组件——布局的用法续二
绝对布局(Ext.layout.container.Absolute) 绝对布局让我回忆到了使用Foxpro开发的时候,哪时候的界面布局就是这样.通过设置控件的左上角坐标(x.y)和宽度来进行的,由于 ...
- 【Demo 0004】屏幕、窗体及视图基础知识
本章学习要点 1. 了解iOS中应用程序(UIApplication)与屏幕.窗体以及视图相关基础知识: 2. 掌握应用程序常用的属性与方法: 3. 掌握窗 ...
- java开发异常类型汇总
1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...
- Test oracle db iops
Today, i need to test one database's iops and do something for oracle db's io test. How to test the ...
- Delphi中WebBrowser自动填表模板
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...
- hibernate级联保存问题,出错not-null property references a null or transient value
Servlet.service() for servlet default threw exception org.hibernate.PropertyValueException: not-null ...
- javascript中使用Map
mis.comm.js.Map = function() { this.elements = new Array(); //获取MAP元素个数 this.size = function() { ret ...
- CentOS下安装MySQL,Windows下使用Navicat for MySql连接
安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查看有没有安装包: yu ...
- 参数传递方法(Delphi1.0与win16API使用pascal方法,即从左到右)
参数传递方法李维的InsideVCL<第一章>中提到Windows定义的回调函数typedef LRESULT (CALLBACK*WNDPROC)(HWND,UNIT,WPARAM,LP ...