在软件系统中。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操作性能对照的更多相关文章

  1. Java IO设计模式彻底分析 (转载)

    一.引子(概括地介绍Java的IO) 无论是哪种编程语言,输入跟输出都是重要的一部分,Java也不例外,而且Java将输入/输出的功能和使用范畴做了很大的扩充.它采用了流的 机制来实现输入/输出,所谓 ...

  2. Java IO(Properties/对象序列化/打印流/commons-io)

    Java IO(Properties/对象序列化/打印流/commons-io) Properties Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载. ...

  3. Java IO之简单输入输出

    Java中的IO分为两个部分,以InputStream和Reader为基类的输入类,以OutputStream和Writer为基类的输出类. 当中InputStream和OutputStream以字节 ...

  4. java IO流体系--通识篇

    1.I/O流是什么 Java的I/O流是实现编程语言的输入/输出的基础能力,操作的对象有外部设备.存储设备.网络连接等等,是所有服务器端的编程语言都应该具备的基础能力. I = Input(输入),输 ...

  5. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

  6. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  7. Java IO之字符流和文件

    前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...

  8. java Io流向指定文件输入内容

    package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...

  9. java Io文件输入输出流 复制文件

    package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...

  10. java Io流更新文件内容

    package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...

随机推荐

  1. Servlet过滤器——仿盗链过滤器

    1.概述 介绍如何使用过滤器技术,防止通过其他URL地址直接访问本站资源.运行本实例,当URL地址不是本站地址时,在网页中将显示错误提示信息. 2.技术要点 主要应用request对象的getHead ...

  2. App开发所要注意的几个法务问题(转)

    GameLook 报道/ 移动应用市场的飞速发展催生出大量揭竿而起的开发者,同时许多矛盾也渐渐明显起来.其中涉及“抄袭”的问题尤为突出,毫不客气地说对于那些有底子的游戏厂商来说,法务已经成为团队中的一 ...

  3. cryptography

    密码关还是有很多变态的题的,整理一下力所能及的吧. Circular Crypto(Asis-CTF2013) 这题只给了一张图片 仔细看一下就知道,这是几个单独的环,把它们分别整理出来.因为看着眼花 ...

  4. mysql 创建函数set global log_bin_trust_function_creators=TRUE;

    <pre name="code" class="html">set global log_bin_trust_function_creators=T ...

  5. 微信JS-SDK说明文档

    http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html http://jsfiddle.net/gabrielerom ...

  6. 键盘皇者 RealForce 104Pro独家评测

    http://tech.sina.cn/?sa=t84d20738943v44&page=2&pwt=rest2&vt=4&from=mbaidu&clickt ...

  7. 怎样取消shutdown关机命令?-shutdown命令的使用解析

    机房上课,可恶电脑总是被管理员測试,不时地弹出这个关机提示.怎样取消这个关机命令呢?其有用 shutdown -a 就可以.以下来学习一下shutdown命令的使用:   shutdown这个命令预计 ...

  8. Android开发者指南-用户界面-拖放-Drag and Drop[原创译文]

      英文原文:http://developer.android.com/guide/topics/ui/drag-drop.html 版本:Android 4.0 r1 译者注:黄色底色为未决译文 快 ...

  9. NET Platform Standard

    NET Platform Standard 相关博文:ASP.NET 5 Target framework dnx451 and dnxcore50 .NET Platform Standard:ht ...

  10. vc 按钮自绘

    按钮自绘,将按钮区域分成三部分,左边.右边.中间都由贴图绘制,可用于手动进度条按钮,或者左右选择项按钮 cpp代码部分: // LRSkinButton.cpp : implementation fi ...