一. 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. abbyy cup a

    link: http://codeforces.com/contest/331/problem/A2 /* ID: zypz4571 LANG: C++ TASK: abby_a.cpp */ #in ...

  2. Socket实现异步通信

    private static void RecVing(IAsyncResult Result) {     //通过 result 获取socket.在这个socket上你启动了BeginAccep ...

  3. leetcode 153. Find Minimum in Rotated Sorted Array --------- java

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. POJ2391 Ombrophobic Bovines(网络流)(拆点)

                         Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. checking

    https://en.wikipedia.org/wiki/Java_performance https://en.wikipedia.org/wiki/Java_virtual_machine ht ...

  6. 利用C#Marshal类实现托管和非托管的相互转换

    Marshal 类 命名空间:System.Runtime.InteropServices 提供了一个方法集,这些方法用于分配非托管内存.复制非托管内存块.将托管类型转换为非托管类型,此外还提供了在与 ...

  7. 安装pgadmin3

    在centos/redhat/系统是x86_64 1.下载适合自己系统的合适的版本http://dl.fedoraproject.org/pub/epel/6/x86_64/ ( yum instal ...

  8. numpy下的flatten()函数用法

    flatten是numpy.ndarray.flatten的一个函数,其官方文档是这样描述的: ndarray.flatten(order='C') Return a copy of the arra ...

  9. easyUI之tabs

    js添加选项卡 $('#box').tabs('add',{option});add是一个方法 对于option来讲,它继承panel,具有它的所有属性.包括id,title,content等. 承前 ...

  10. jQuery中each的用法之退出循环和结束本次循环

    jQuery中each的用法之退出循环和结束本次循环 jQuery中each类似于javascript的for循环 但不同于for循环的是在each里面不能使用break结束循环,也不能使用conti ...