MappedByteBuffer读写文件
一. MappedByteBuffer
java把文件映射到内存中,避免堆内存产生大对象引起full gc。mappedByteBuffer的读写速度都要超过堆内读写文件的速度
public class AA {
public static void main(String[] args) throws IOException, InterruptedException {
map();
//fis();
} private static void fis() throws IOException {
/*long l = System.currentTimeMillis();
FileOutputStream fos = new FileOutputStream(new File("d://1234.txt"));
for(int i=0;i<4000000;i++)
fos.write('a');
System.out.println((System.currentTimeMillis()-l)); //13011*/ long l = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(new File("d://1234.txt"));
int read;
for(int i=0;i<4000000;i++) {
read = fis.read();
}
System.out.println((System.currentTimeMillis()-l)); //9774
} private static void map() throws IOException {
/*long l = System.currentTimeMillis();
FileChannel fileChannel = new RandomAccessFile("d://1234.txt", "rw").getChannel();
CharBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 4000000*2).asCharBuffer();
System.out.println("start");
for(int i=0;i<4000000;i++)
mappedByteBuffer.put('a');
System.out.println((System.currentTimeMillis()-l));
fileChannel.close(); //47*/ long l = System.currentTimeMillis();
FileChannel fileChannel = new RandomAccessFile("d://1234.txt", "r").getChannel();
CharBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()).asCharBuffer();
System.out.println("start");
for(int i=0;i<4000000;i++)
mappedByteBuffer.get();
System.out.println((System.currentTimeMillis()-l));
fileChannel.close(); //40
}
}关闭MappedByteBuffer
public final class IOUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(IOUtil.class); private IOUtil() {
} public static void closeMappedByteBuffer(final MappedByteBuffer byteBuffer) {
if (byteBuffer == null) {
return;
} if (byteBuffer != null) {
AccessController.doPrivileged(
new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner", new Class[0]);
cleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) cleanerMethod.invoke(byteBuffer,
new Object[0]);
cleaner.clean();
} catch (NoSuchMethodException e) {
LOGGER.error("error occurred when clean mapped byte buffer", e);
} catch (InvocationTargetException e) {
LOGGER.error("error occurred when clean mapped byte buffer", e);
} catch (IllegalAccessException e) {
LOGGER.error("error occurred when clean mapped byte buffer", e);
}
return null;
}
}
);
}
}
}
MappedByteBuffer读写文件的更多相关文章
- RandomAccessFile、FileChannel、MappedByteBuffer读写文件
s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...
- 顺序、随机IO和Java多种读写文件性能对比
概述 对于磁盘的读写分为两种模式,顺序IO和随机IO. 随机IO存在一个寻址的过程,所以效率比较低.而顺序IO,相当于有一个物理索引,在读取的时候不需要寻找地址,效率很高. 基本流程 总体结构 我们编 ...
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...
- Python读写文件
Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...
- php中并发读写文件冲突的解决方案
在这里提供4种高并发读写文件的方案,各有优点,可以根据自己的情况解决php并发读写文件冲突的问题. 对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果 ...
- C#读写文件的方法汇总_C#教程_脚本之家
C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm
- Inno Setup 如何读写文件
软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...
- java使用IO读写文件总结
每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...
随机推荐
- TextView文字滚动效果
ScrollText.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&quo ...
- JS控制文本框textarea输入字数限制
<html> <head> <title>JS限制Textarea文本域字符个数</title> <meta http-equiv="C ...
- 全国信息学奥林匹克联赛 ( NOIP2014) 复赛 模拟题 Day1 长乐一中
题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer. ...
- mysql创建用户和创建数据库赋值用户操作权限
mysql 进入mysqldb SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; ...
- 课堂所讲整理:HTML--6运算符、类型转换
1.类型转换: 分为自动转换和强制转换,一般用强制转换. 其他类型转换为整数:parseint(): 其他类型转换为小数:parsefloat(): 判断是否是一个合法的数字类型:isNaN(): 是 ...
- 最大子段和问题,最大子矩阵和问题,最大m子段和问题
1.最大子段和问题 问题定义:对于给定序列a1,a2,a3……an,寻找它的某个连续子段,使得其和最大.如( -2,11,-4,13,-5,-2 )最大子段是{ 11,-4,13 }其和为2 ...
- iPad 2升级iOS 9的过程记录
有一台老旧的iPad2,iOS版本还是5.1.1,现在好多软件都无法安装了. 决定升级到最新的操作系统,中间的过程,遇到的问题和解决办法如下: 据说升级到iOS 9以后就不好越狱了,不过我也就是用用一 ...
- vsftp虚拟用户配置
找了很久,终于找到像样一点的文章,很详细,参数方面懂英文基本能看懂,一个教程是否有用,关键在于细节.错了一点点就不能配下去了. ------------------------------------ ...
- SyntaxError: Non-ASCII character '\xe6'
这是编码的问题,在文件第一行加上如下命令即可: #encoding: utf-8
- C#中IP地址转换为数值的方法
任何语言都通用的方法转换 IP 地址 a.b.c.d ==> a***+b**+c*+d ===> *(c+*(b+*a)) +d 示例: ***+**+*+ ===> *( +*( ...