一. 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. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. Android——不同activity之间数据传递

    /* * 不同activity之间数据的传递 */ public class MainActivity extends Activity { private EditText et_name; @Ov ...

  3. 在IE地址栏输入JS的有趣效果

    1.编辑网页 在地址栏输入下面的代码按enter,网页上所有元素都能变成可编辑状态,你可以移动.调整元素大小.如果你只是讨厌某个网站想发泄一下,我建议你使用NetDisater. 代码如下: java ...

  4. Android sdk 镜像服务器资源

    大连东软信息学院镜像服务器地址:- http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:- IPv4: http://ubuntu.buct.edu.cn ...

  5. android:id="@+id/button1" 与 android:id="@id/button1" 区别 @string

    一.android:id="@+id/button1" 与 android:id="@id/button1" 区别 android:id="@+id/ ...

  6. POI导入

    public void import(){ XSSFWorkbook wb = new XSSFWorkbook(new File("filePath")); XSSFSheet ...

  7. fs event_socket

    mod_event_socket     Skip to end of metadata   Created by John Boteler, last modified by Niek Vlesse ...

  8. javascript输出图的简单路径

    <script> //图的构建 function vnode() { this.visited=0; this.vertex=0; this.arcs=new Array(); } fun ...

  9. C#学习之LinqtoSql类的简单例子

    LinqtoSql类把访问.操作数据库的细节封装了起来,把连接操作数据库变得相当简单.下面是简单的例子. 第一步:添加LinqtoSql类 1.创建一个控制台应用程序项目,下载一个NrothWind ...

  10. vb6动态创建webbrowser

    Option Explicit Private WithEvents IE As VBControlExtender Private Sub Command1_Click() Dim IE Set I ...