NIO FileChannel
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的更多相关文章
- Java NIO FileChannel
		A Java NIO FileChannel is a channel that is connected to a file. Using a file channel you can read d ... 
- JAVA NIO FileChannel 内存映射文件
		文件通道总是阻塞式的. 文件通道不能创建,只能通过(RandomAccessFile.FileInputStream.FileOutputStream)getChannel()获得,具有与File ... 
- 【原创】java NIO FileChannel 学习笔记 FileChannel实现分析 即FileChannelImpl分析
		上文已经说了FileChannel是一个抽象类,FileChannelImpl是其实现,接下来介绍FileChannelImpl,参考代码来自OpenJDK7 首先 public class File ... 
- 【原创】java NIO FileChannel 学习笔记 FileChannel 简介
		java NIO 中FileChannel 的实现类是 FileChannelImpl,FileChannel本身是一个抽象类. 先介绍FileChannel File Channels 是线程安全 ... 
- nio FileChannel中文乱码问题
		最近用nio读取文件时,英文正常,读取中文时会出现乱码,经查可以用Charset类来解决: 代码如下: package com.example.demo; import java.io.FileNot ... 
- 【原创】java NIO FileChannel 学习笔记 新建一个FileChannel
		首先使用FileChannel 的open方法获取一个FileChannel对象.下面这段代码是FileChannel中open方法的代码. public static FileChannel ope ... 
- Java NIO:FileChannel数据传输
		调用方式 FileChannel dstChannel; FileChannel srcChannel; dstChannel.transferFrom(srcChannel,0,srcChannel ... 
- 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 ... 
- Java NIO学习笔记五       FileChannel(文件通道)
		Java NIO FileChannel Java NIO FileChannel是连接文件的通道.使用FileChannel,您可以从文件中读取数据和将数据写入文件.Java NIO FileCha ... 
随机推荐
- 201521123074 《Java程序设计》第14周学习总结
			1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 Q1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现 ... 
- 201521123009 《Java程序设计》第12周学习总结
			1. 本周学习总结 2. 书面作业 Q1:将Student对象(属性:int id, String name,int age,double grade)写入文件student.data.从文件读出显示 ... 
- 利用ASCII码生成指定规则的字符串
			/** * 上送终端编号的后两位生成规则 总共可以生成 (36*36-1)1295个编号 * 01...09 0A...0Z * 10...19 1A...1Z * ............... * ... 
- [js高手之路] es6系列教程 - promise常见用法详解(resolve,reject,catch,then,all,race)
			关于promise我在之前的文章已经应用过好几次,如[js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist,本文就来讲解下pro ... 
- 关于Tomcat一些启动错误的解决方法
			一.Eclipse tomcat 启动超时: 错误内容: Server JBoss v4.0 at localhost was unable to start within 50 seconds. I ... 
- Akka(27): Stream:Use case-Connecting Slick-dbStream & Scalaz-stream-fs2
			在以前的博文中我们介绍了Slick,它是一种FRM(Functional Relation Mapper).有别于ORM,FRM的特点是函数式的语法可以支持灵活的对象组合(Query Composit ... 
- 学习ExtJS的grid布局
			这是之前学习ExtJS布局的时候我导师让我重点熟悉的内容.之后会发一个最近写的结合MVC项目的grid布局的案例. 上一篇关于ExtJS的学习资料什么的都已经更在上一篇了,这里只是对一些代码的记录. ... 
- QCW切割   --铁片
			1.QCW切割旋转轴限位部件 --刘锦峰协助 :铁片 功率85% 最大功率100 最小功率50 脉宽0.1ms 调整焦点-0.5左右 
- 概率图模型PGM——D map, I map, perfect map
			若F分布的每个条件独立性质都反映在A图中,则A图被称为F分布的D map. 若A图表现出的所有条件独立性质都在F分布中满足(与F分布不矛盾),则A图被称为F分布的I map. 弱A图既是F分布的D m ... 
- 简单的CSS颜色查看工具
			可以通过输入ARGB(A代表透明度)格式或者HEX格式查看颜色,也可以进行ARGB格式和者HEX格式转换,如下图 使用C#编写,我已将源代码压缩上传 下载地址:http://files.cnblogs ... 
