JAVA I/O 、NIO 文件读写性能对比
来源:互联网 发布:java spring 单例模式 编辑:程序博客网 时间:2024/06/08 11:25
NIO将文件映射到内存进行读写的性能会比普通的IO读写快很多!
下面就写个测试的demo,代码如下:
package utils.nio;import java.io.*;import java.nio.ByteBuffer;import java.nio.CharBuffer;import java.nio.channels.FileChannel;/** * 文件名称: FileReadWriteTest.java * 编写人: yh.zeng * 编写时间: 17-6-8 下午9:18 * 文件描述: IO和NIO文件读写性能对比 */public class FileReadWriteTest{ public static void main(String args[]) throws Exception{ FileReadWriteTest test = new FileReadWriteTest(); long start = System.currentTimeMillis(); //for(int i = 0; i <= 10; i++){ test.ioCopyFile("F://file.txt","F://filecopy.txt"); //} System.out.println("IO使用缓冲区耗时:" + (System.currentTimeMillis() - start) + "ms"); start = System.currentTimeMillis(); //for(int i = 0; i <= 10; i++){ test.nioCopyFile("F://file.txt", "F://filecopy.txt"); //} System.out.println("NIO使用缓冲区读写耗时:" + (System.currentTimeMillis() - start) + "ms"); start = System.currentTimeMillis(); //for(int i = 0; i <= 10; i++){ test.nioCopyFile2("F://file.txt", "F://filecopy.txt"); //} System.out.println("NIO映射到内存读写文件耗时:" + (System.currentTimeMillis() - start) + "ms"); } /** * NIO读写通道(双通道)进行文件的读写操作,数据使用一个缓冲区 * @param source 源文件 * @param target 目标文件 * @throws Exception */ public void nioCopyFile(String source, String target) throws Exception{ FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(target); FileChannel readChannel = fis.getChannel(); //读文件通道 FileChannel writeChannel = fos.getChannel(); //写文件通道 ByteBuffer buffer = ByteBuffer.allocate(1024*8); //缓冲区 buffer.clear(); while (readChannel.read(buffer) != -1){ buffer.flip(); writeChannel.write(buffer); buffer.clear(); } readChannel.close(); writeChannel.close(); fis.close(); fos.close(); } /** * NIO 将文件映射到内存进行读和写 * @param source 源文件 * @param target 目标文件 * @throws Exception */ public void nioCopyFile2(String source, String target) throws Exception{ FileInputStream fis = new FileInputStream(source); FileChannel readChannel = fis.getChannel(); //读文件通道 FileChannel writeChannel = new RandomAccessFile(target,"rw").getChannel(); //写文件通道 int fileLen = fis.available(); CharBuffer charReadBuffer = readChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileLen).asCharBuffer(); CharBuffer charWriteBuffer = writeChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileLen).asCharBuffer(); charWriteBuffer.put(charReadBuffer); readChannel.close(); fis.close(); writeChannel.close(); } /** * IO读写文件,使用了缓冲区 * @param source 源文件 * @param target 目标文件 * @throws Exception */ public void ioCopyFile(String source, String target) throws Exception{ FileInputStream fis = new FileInputStream(source); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream(target); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] buffer = new byte[1024*8]; //缓冲区 int lenth = 0; while ((lenth = bis.read(buffer)) != -1){ bos.write(buffer, 0 , lenth); bos.flush(); } fis.close(); bis.close(); fos.close(); bos.close(); }}
上述代码运行结果(demo使用的文件file.txt的大小为5M):
IO使用缓冲区耗时:25msNIO使用缓冲区读写耗时:38msNIO映射到内存读写文件耗时:10msProcess finished with exit code 0
阅读全文
0 0
- JAVA I/O 、NIO 文件读写性能对比
- java对比IO和NIO的文件读写性能测试
- Java中Stream和NIO的Memory-mapped File的I/O性能对比
- java I/O 读写文件
- java读写文件时nio、bio对比
- NIO与普通IO文件读写性能对比
- java 读写文件(I/O)
- Java I/O 概述---文件读写总结
- Java I/O系统 文件读写
- Java: NIO(new I/O)
- NIO(java new I/O)
- Java NIO 读写文件
- java NIO读写文件
- java NIO读写文件
- JAVA NIO 读写文件
- C++ 文件读写 I/O
- I/O流 文件读写
- 文件I/O流读写
- MySQL
- matlab倒序打印
- Hadoop作业提交分析(一)
- STM32时钟之3
- Spark Streaming小结
- JAVA I/O 、NIO 文件读写性能对比
- C++迭代器
- continue
- Express 创建项目命令,怎么使用ejs模板?
- oracle 如何获取今天是星期几
- git 更新本地分支与远程同步
- mongod副本集部署
- 第三十九天:Aurora下拉框的三种获取数据源的形式
- 前端神器-----CodePen