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 ...
随机推荐
- [js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist
promise主要是用来解决异步回调问题,其实还有好几种比promise更好的方案,后面再说,这节,我们先用promise来改造下,我以前写的一篇文章[js高手之路]javascript腾讯面试题学习 ...
- Tomcat配置用户名和密码
1.在浏览器输入http://localhost:8080/,打开Tomcat自带的默认主页面,右侧有"administration""documentation&quo ...
- 微软云linux服务器FTP文件传输错误解决办法
在微软云上新建了linux虚拟机之后,通过Xshell连接到服务器(微软云默认的账号是:azureuser,不是root),却发现通过FTP传输文件错误,一直找不到头绪,询问微软云相关人员才知道.FT ...
- 安装 node-sass 时报错
在安装 node-sass 时报错,截图如下 解决方法如下: npm install --save node-sass --registry=https://registry.npm.taobao.o ...
- JS -- Variables As Properties
Variables As Properties When you declare a global JavaScript variable, what you are actually doing i ...
- php里的抽象类和接口
//实例化类产生对象.//class fenbi//{// //普通成员,属于对象// public $length = "10cm";// //静态成员,静态变量,属于类.// ...
- jvm内存分配和回收策略
在上一篇中,已经介绍了内存结构是什么样的. 这篇来介绍一下 内存是怎么分配的,和怎么回收的.(基本取自<深入理解Java虚拟机>一书) java技术体系中所提倡的自动内存管理最终可以归结为 ...
- 从头编写 asp.net core 2.0 web api 基础框架 (3)
第一部分:http://www.cnblogs.com/cgzl/p/7637250.html 第二部分:http://www.cnblogs.com/cgzl/p/7640077.html 之前我介 ...
- JS类继承常用方式发展史
JS类继承常用方式发展史 涉及知识点 构造函数方式继承 1-继承单个对象 1.1 多步走初始版 1.2 多步走优化版 1.3 Object.create()方式 2-继承多个对象 2.1 遍历 Obj ...
- asp.net或者MVC定时自动执行某操作
using System; using System.IO; using System.Runtime.CompilerServices; using System.Text; namespace S ...