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 ...
随机推荐
- ubuntu 以root 运行程序
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- linux 网络协议分析---3
本章节主要介绍linxu网络模型.以及常用的网络协议分析以太网协议.IP协议.TCP协议.UDP协议 一.网络模型 TCP/IP分层模型的四个协议层分别完成以下的功能: 第一层 网络接口层 网络接口层 ...
- UVa 400
一开始没怎么看懂题目,原来就是M字符就是这一列的宽度为M个字符,包括空格. #include<iostream> #include<algorithm> #include< ...
- phpmyadmin使用中碰到的一些问题
在导入数据库文件的时候出现 #1062 – Duplicate entry '1′ for key ‘PRIMARY' 说明在上一次的导入中没有完全导入,但是主键是自增的,所以要输入主键才能继续,解决 ...
- 越狱Season 1-Episode 1: the pilot
the pilot: 美国电视剧新剧开播都会有一个试播来测试观众对新剧的接受程度,以此来决定是否再继续播下去,也可以说是一个开端,第一集,试播 -Tattoo Artist: That's it. t ...
- 记录Cat类的个体数目
B.记录Cat类的个体数目 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 22 (17 users) Total Accepted: ...
- kuangbin_ShortPath S (POJ 3169)
被cow类题目弄得有些炸裂 想了好久好久写了120多行 依然长跪不起发现计算约束条件的时候还是好多麻烦的地方过不去 然后看了看kuangbin的blog 都是泪啊 差分约束的方式做起来只要70多行啊炒 ...
- java的nio之:java的nio系列教程之概述
一:java的nio的核心组件?Java NIO 由以下几个核心部分组成: ==>Channels ==>Buffers ==>Selectors 虽然Java NIO 中除此之外还 ...
- DuiLib——第一篇UIManager
DUiLib 源码分析 --以UiLib 1.01版为分析目标 -------------------------------------------------------------------- ...
- HashMap和HashSet的区别
理解HashSet及使用 HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到Collection框架以及多线程的面试,可以说是不完整.而Collection框架的问题 ...