NIO提供了比传统的文件访问更好的访问方法,NIO有两个优化的方法:一个是 FIleChannel.transferTo FileChannel.transferFrom,另一个是FileChannel.map,均提供了数据在内核空间的直接移动,减少了内核空间和用户空间的复制损耗

下面是FileChannel.map使用示例:

    public static void main(String[] args) {
        int BUFFER_SIZE = 1024;
        String filename = "teset.db";
        long fileLength = new File(filename).length();
        int bufferCount = 1 + (int) fileLength / BUFFER_SIZE;
        MappedByteBuffer[] buffers = new MappedByteBuffer[bufferCount];
        long remaining = fileLength;
        for (int i=0; i<bufferCount; i++) {
            RandomAccessFile file;
            try {
                file = new RandomAccessFile(filename, "r");
                buffers[i] = file.getChannel().map(FileChannel.MapMode.READ_ONLY, i*BUFFER_SIZE, (int) Math.min(remaining, BUFFER_SIZE));
            } catch (Exception e) {
                e.printStackTrace();
            }
            remaining -= BUFFER_SIZE;
        }
    }

NIO FileChannel的更多相关文章

  1. Java NIO FileChannel

    A Java NIO FileChannel is a channel that is connected to a file. Using a file channel you can read d ...

  2. JAVA NIO FileChannel 内存映射文件

      文件通道总是阻塞式的. 文件通道不能创建,只能通过(RandomAccessFile.FileInputStream.FileOutputStream)getChannel()获得,具有与File ...

  3. 【原创】java NIO FileChannel 学习笔记 FileChannel实现分析 即FileChannelImpl分析

    上文已经说了FileChannel是一个抽象类,FileChannelImpl是其实现,接下来介绍FileChannelImpl,参考代码来自OpenJDK7 首先 public class File ...

  4. 【原创】java NIO FileChannel 学习笔记 FileChannel 简介

    java NIO 中FileChannel 的实现类是  FileChannelImpl,FileChannel本身是一个抽象类. 先介绍FileChannel File Channels 是线程安全 ...

  5. nio FileChannel中文乱码问题

    最近用nio读取文件时,英文正常,读取中文时会出现乱码,经查可以用Charset类来解决: 代码如下: package com.example.demo; import java.io.FileNot ...

  6. 【原创】java NIO FileChannel 学习笔记 新建一个FileChannel

    首先使用FileChannel 的open方法获取一个FileChannel对象.下面这段代码是FileChannel中open方法的代码. public static FileChannel ope ...

  7. Java NIO:FileChannel数据传输

    调用方式 FileChannel dstChannel; FileChannel srcChannel; dstChannel.transferFrom(srcChannel,0,srcChannel ...

  8. Java NIO read/write file through FileChannel

    referee:  Java NIO FileChannel A java nio FileChannel is an channel that is connected to a file. Usi ...

  9. Java NIO学习笔记五 FileChannel(文件通道)

    Java NIO FileChannel Java NIO FileChannel是连接文件的通道.使用FileChannel,您可以从文件中读取数据和将数据写入文件.Java NIO FileCha ...

随机推荐

  1. 201521123103 《java学习笔记》 第十二周学习总结

    一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 二.书面作业 将Student对象(属性:int id, String name,int age,double ...

  2. 201521123052《Java程序设计》第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...

  3. sourcetree和gitlab配置图解

    一.前期准备安装 1.git客户端(1.产生gitlab服务端和本地git相互传输时所需要校验的私钥和公钥    2.直接在Idea中使用git提交和push代码,当然也可以用sourcetree提交 ...

  4. webservice03#schema#元素属性定义

    工具软件XMLSpy 2010 破解版,是非常好的写XMl的工具软件. 1,Schema的好处: Schema出现的目的是通过一个更加合理的方式来编写xml的限制文件(基于xml语法的方式): Sch ...

  5. 关于sublime3的使用

    一.安装Package Control 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码: import urllib.request,os; pf ...

  6. vue+axios 前端实现登录拦截(路由拦截、http拦截)

    一.路由拦截 登录拦截逻辑 第一步:路由拦截 首先在定义路由的时候就需要多添加一个自定义字段requireAuth,用于判断该路由的访问是否需要登录.如果用户已经登录,则顺利进入路由, 否则就进入登录 ...

  7. 自己写实现char TO wchar_t 的转换

    wchar_t CharToWChart(char nChar){    wchar_t nR;    nR=nChar+32*256;    return nR;}//--------------- ...

  8. Oracle第一波

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  9. 配置 VirtualBox backend - 每天5分钟玩转 Docker 容器技术(75)

    Rexy-Ray 支持多种 backend,上一节我们已经安装配置了 Rex-Ray,今天演示如何配置 VirtualBox backend. 在 VirtualBox 宿主机,即我的笔记本上启动 v ...

  10. FZU 1919 -- K-way Merging sort(记忆化搜索)

    题目链接 Problem Description As we all known, merge sort is an O(nlogn) comparison-based sorting algorit ...