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. 201521123028 《Java程序设计》第10周学习总结

    1. 本周学习总结 异常方面,主要是调试的相关内容,断点的使用,常用快捷键(F5(step into) F6(step over,跳过),F7 (step return,跳出)). 多线程: ①.进程 ...

  2. 数据库系统概论——Chap. 1 Introduction

    数据库系统概论--Introduction 一.数据库的4个基本概念 数据(data):数据是数据库中存储的基本单位.我们把描述事物的符号记录称为数据.数据和关于数据的解释是不可分的,数据的含义称为数 ...

  3. 存储过程重置SEQUENCE值从新开始。

    CREATE OR REPLACE PROCEDURE RESET_SEQUENCE( v_SeqName IN VARCHAR2, v_sqlcode OUT NUMBER, v_sqlerrm O ...

  4. Hibernate哪点事?

    1.为什么在Hibernate的实体类中要提供一个无参数的构造器这一点非常重要?每个Hibernate实体类必须包含一个 无参数的构造器, 这是因为Hibernate框架要使用Reflection A ...

  5. csv文件读取

    from urllib.request import urlopen from io import StringIO import csv data = urlopen("http://py ...

  6. win10- *.msi 软件的安装,比如 SVN安装报2503,2502

    1, 以管理员身份打开cmd ,   C:\Windows\System32\cmd.exe 2,输入: msiexec /package "安装文件的全路径" 3,按下回车. 例 ...

  7. Java单链表实现

    /** * * 单链表基本操作 * * @author John * */ class LinkList { private Node first; private int pos = 0; publ ...

  8. web自动化1-selenium简介及环境搭建

    selenium 开源软件 支持多浏览器Firefox.Chrome.IE 跨平台Windows.Mac.Linux 多语言 java python Ruby php js 对web支持好,多种API ...

  9. 0 can't find referenced pointcut declarePointExpress

    今天在用SpringAOP 的 @pointCut 的时候报错 Exception in thread "main" org.springframework.beans.facto ...

  10. OC——继承

    继承的其中一个很重要的目的是为了实现多态.我们现在先来看看OC的继承. 一.继承 父类: 头文件 // // Peason.h // 01-继承和多态 // // Created by zhangji ...