一. MappedByteBuffer

  1. 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
    }
    }
  2. 关闭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读写文件的更多相关文章

  1. RandomAccessFile、FileChannel、MappedByteBuffer读写文件

    s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...

  2. 顺序、随机IO和Java多种读写文件性能对比

    概述 对于磁盘的读写分为两种模式,顺序IO和随机IO. 随机IO存在一个寻址的过程,所以效率比较低.而顺序IO,相当于有一个物理索引,在读取的时候不需要寻找地址,效率很高. 基本流程 总体结构 我们编 ...

  3. Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...

  4. 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...

  5. Python读写文件

    Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...

  6. php中并发读写文件冲突的解决方案

    在这里提供4种高并发读写文件的方案,各有优点,可以根据自己的情况解决php并发读写文件冲突的问题. 对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果 ...

  7. C#读写文件的方法汇总_C#教程_脚本之家

    C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm

  8. Inno Setup 如何读写文件

    软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...

  9. java使用IO读写文件总结

    每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...

随机推荐

  1. 网站后台的lnmp启动与重启

    网站建立时间很长了,经常挂掉,又没有其他技术人员带.只好自己摸索着修复. 到今天网站已经挂掉了一个礼拜.请求各路大神无果后决定自己修复. 首先出现的是502,网关错误. 1.上阿里云服务用户中心重新启 ...

  2. IOS中打开应用实现检查更新的功能

    //检查更新页面 - (void)Renew{        NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];        ...

  3. meta是什么意思?

    META标签,是HTML语言head区的一个辅助性标签.在几乎所有的page里,我们都可以看 到类似下面这段html代码: -------------------------------------- ...

  4. C++ 遇到的问题小结

    1. cannot convert 'std::basic_string<char>' to 'int' in assignment ... 原始code如下: int id2; std: ...

  5. GPU的革命

    CUDA 线程执行模型分析(一)招兵------ GPU的革命 CUDA 线程执行模型分析(二)大军未动粮草先行------GPU的革命 CUDA硬件实现分析(一)------安营扎寨-----GPU ...

  6. 执行bat文件

    set CLASSPATH=E:\kuaipan\work\J2SE_workspace\JavaSEbasic\bin;E:\kuaipan\study\jar\jxl\*; set HEAP=-X ...

  7. python--web.py使用

    web.py 是一个轻量级Python web框架. 下面我将使用web.py框架,创建一个简单的html页面示例. 1.项目的目录结构如下所示: exweb2\  uniqueenv\  app.p ...

  8. 【转】GridView中页脚汇总显示

    来源:http://blog.csdn.net/atian15/article/details/3495514 有时候需要在GridView的页脚中汇总显示一些信息,常见的方法有两种: 1.在SQL中 ...

  9. js 二维数组定义

    1.二维数组声明方式是下面这样的: var images=new Array(); //先声明一维 for(var i=0;i<10;i++){ //一维长度为10 images[i]=new ...

  10. .net托管平台appharbor使用

    这篇文章是网上转过来的,在AppHarbor使用Git上传Code的时候,需要输入用户名和密码,就是appharbor登陆的用户名和密码. 可以参考这篇文章http://www.freehao123. ...