RandomAcessFile、MappedByteBuffer和缓冲读/写文件
项目需要进行大文件的读写,调查测试的结果使我决定使用MappedByteBuffer及相关类进行文件的操作,效果不是一般的高。
网上参考资源很多,如下两篇非常不错:
1、花1K内存实现高效I/O的RandomAccessFile类
2、Java中Stream和Memory-mapped File的I/O性能对比
小结:
1、RandomAccessFile本身不带缓冲读写,和FileInputStream、FileOutputStream等一样,直接按字节读写时,性能不可接受;
2、使用MappedByteBuffer读写,固然性能会得到极大提升;其实只要自己处理缓冲,性能都会有非常大的提升,比如以下两种方式(见下记代码)中第一种使用了MappedByteBuffer,第二种自己进行缓冲,对于12M的文件,后者的效率甚至高于前者。
3、BufferedXXXX之类的缓冲流,如果仅使用默认的buffer size,性能不一定最优,要权衡不同情况各种因素设置大小。
package helloword.helloword; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; public class Test { public static void main(String[] args) {
// TODO Auto-generated method stub } public void test() throws IOException {
/*
* 测试结果与Buffer size有关
*/
// 1、使用MappedByteBuffer: 0.7s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
RandomAccessFile rafi = new RandomAccessFile(srcFile, "r");
RandomAccessFile rafo = new RandomAccessFile(destFile, "rw");
FileChannel fci = rafi.getChannel();
FileChannel fco = rafo.getChannel();
long size = fci.size();
MappedByteBuffer mbbi = fci.map(FileChannel.MapMode.READ_ONLY, 0, size);
MappedByteBuffer mbbo = fco.map(FileChannel.MapMode.READ_WRITE, 0, size);
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
byte b = mbbi.get(i);
mbbo.put(i, b);
}
fci.close();
fco.close();
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); } public void test2() throws IOException {
// 2、自己处理Buffer(RandomAccessFile): 0.13s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
RandomAccessFile rafi = new RandomAccessFile(srcFile, "r");
RandomAccessFile rafo = new RandomAccessFile(destFile, "rw"); byte[] buf = new byte[1024 * 8]; long start = System.currentTimeMillis(); int c = rafi.read(buf); while (c > 0) {
if (c == buf.length) {
rafo.write(buf);
} else {
rafo.write(buf, 0, c);
} c = rafi.read(buf);
}
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s"); } public void test3() throws IOException {
// 3、BufferedInputStream&BufferedOutputStream: 3.02s
String srcFile = "D://Noname1.txt";
String destFile = "D://copy.txt";
FileInputStream rafi = new FileInputStream(srcFile);
FileOutputStream rafo = new FileOutputStream(destFile); BufferedInputStream bis = new BufferedInputStream(rafi, 8192);
BufferedOutputStream bos = new BufferedOutputStream(rafo, 8192);
long size = rafi.available(); long start = System.currentTimeMillis(); for (int i = 0; i < size; i++) {
byte b = (byte) bis.read();
bos.write(b);
}
rafi.close();
rafo.close();
System.out.println("Spend: " + (double) (System.currentTimeMillis() - start) / 1000 + "s");
}
}
RandomAcessFile、MappedByteBuffer和缓冲读/写文件的更多相关文章
- java读/写文件
读取文件参考:https://blog.csdn.net/weixin_42129373/article/details/82154471 写入文件参考:https://blog.csdn.net/B ...
- C++ 二进制文件 读 写文件
1 #include <iostream> 2 #include <string> 3 #include<fstream> 4 using namespace st ...
- read(),write() 读/写文件
read read()是一个系统调用函数.用来从一个文件中,读取指定长度的数据到 buf 中. 使用read()时需要包含的头文件: <unistd.h> 函数原型: ssize_t re ...
- 关于使用 Java 分片读\写文件
分片读取文件方法: /** * 分片读取文件块 * * @param path 文件路径 * @param position 角标 * @param blockSize 文件块大小 * @return ...
- c# 读/写文件(各种格式)
最简单的: --------写 //content是要写入文本的字符串 //(@txtPath + @"\" + rid + ".txt");要被写入的TXT ...
- Java基础之写文件——通过缓冲流写文件(StreamOutputToFile)
控制台程序,生成一些二进制整型值并且将它们写入到文件中. import java.nio.file.*; import java.nio.*; import java.io.*; public cla ...
- Pandas 基础(4) - 读/写 Excel 和 CSV 文件
这一节将分别介绍读/写 Excel 和 CSV 文件的各种方式: - 读入 CSV 文件 首先是准备一个 csv 文件, 这里我用的是 stock_data.csv, 文件我已上传, 大家可以直接下载 ...
- 【练习】Java中的读文件,文件的创建,写文件
前言 大家好,给大家带来Java中的读文件,文件的创建,写文件的概述,希望你们喜欢 读文件 public static void read(String path,String filename){ ...
- py库: xlwt 、xlrd (写读EXCEL文件)
写EXCEL文件 # -*- coding: utf-8 -*- import xlwt book = xlwt.Workbook(encoding = "utf-8", styl ...
随机推荐
- Hadoop教程之编写HelloWorld(2)
前面我们写了一个Hadoop程序,并让它跑起来了.但想想不对啊,Hadoop不是有两块功能么,DFS和MapReduce.没错,上一节我们写了一个MapReduce的HelloWorld程序,那这一节 ...
- Android TextView 文字居中
有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...
- ECharts本地部署
将下载的包解压,再将包内文件copy到C:\inetpub\wwwroot下 在html中做如下引用: <script type="text/javascript" sr ...
- Linux系统文件权限&目录权限
linux系统一切都是文件,文件和目录的所属与权限--来分别规定所有者.所有组.其余人的读.写.执行权限. 读(read),写(write),执行(excute)简写为(r.w.x),也可以以用(4. ...
- javascript ajax的语法
ajax参数: 详细参数转到如下地址: http://www.w3school.com.cn/jquery/ajax_ajax.asp $.ajax语法: jQuery.ajax([settings] ...
- DOJO 八 event dojo/on
官网教程:Events with Dojo在元素上绑定events,需要引用包dojo/on,通过on方法来实现. <button id="myButton">Clic ...
- Kernel rest_init相关
Linux系统里,有些进程只有kernel部分的代码,即由一个kernel函数进入,在sched的时候,将其与用户进程同等对待. PID为0的叫swapper或sched进程,对应函数为rest_in ...
- Linux Kernel CMPXCHG函数分析
原文地址:http://blog.csdn.net/penngrove/article/details/44175387 最近看到Linux Kernel cmpxchg的代码,对实现很不理解.上网查 ...
- cmd.exe-应用程序错误 应用程序无法正常启动(0xc0000142)
之前还好好的,突然就遇到这个问题,运行CMD报错(如上图),后面无论怎么重启都是这样. 导致所有与CMD相关的程序任务都出错,例如Ctrl+Alt+Delete 只好开始各种百度谷歌 找到如下几种解决 ...
- 函数lock_rec_create
/*********************************************************************//** Creates a new record lock ...