SeekableByteChannel 和 FileChannel
Java7中引入了SeekableByteChannel接口,允许我们定位到文件的任意位置进行读写。注意这里的写,不是新增式的插入,而是覆盖,当然在文件末尾的写,是新增。
java.nio.channels.SeekableByteChannel A byte channel that maintains a current position and allows the position to be changed. A seekable byte channel is connected to an entity, typically a file, that contains a variable-length sequence of bytes
that can be read and written. The current position can be queried and modified. The channel also provides access to
the current size of the entity to which the channel is connected. The size increases when bytes are written beyond its
current size; the size decreases when it is truncated. The position and truncate methods which do not otherwise have a value to return are specified to return the channel
upon which they are invoked. This allows method invocations to be chained. Implementations of this interface should
specialize the return type so that method invocations on the implementation class can be chained.
而文件通道:FileChannel实现了该接口:
java.nio.channels.FileChannel A channel for reading, writing, mapping, and manipulating a file. A file channel is a SeekableByteChannel that is connected to a file. It has a current position within its file which
can be both queried and modified. The file itself contains a variable-length sequence of bytes that can be read and
written and whose current size can be queried. The size of the file increases when bytes are written beyond its
current size; the size of the file decreases when it is truncated. The file may also have some associated metadata
such as access permissions, content type, and last-modification time; this class does not define methods for metadata access.
实例:
Path filePath = Paths.get("D://error.log");
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put("111111".getBytes());
buffer.flip();
try {
FileChannel channel = FileChannel.open(filePath, StandardOpenOption.WRITE);
channel.position(80); // 覆盖此处的内容为111111
channel.write(buffer);
channel.close();
buffer = ByteBuffer.allocate(1024);
channel = FileChannel.open(filePath, StandardOpenOption.READ);
channel.position(100); // 读取此处的内容
channel.read(buffer, 10);
buffer.flip();
System.out.println(buffer);
Charset charset = Charset.forName("utf-8");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer);
System.out.println(charBuffer);
} catch (IOException e) {
e.printStackTrace();
}
注意上面的例子中涉及到 ByteBuffer 到 CharBuffer 的转换,也就是字节到字符的转换,需要使用对应的编码的解码器进行解码。
SeekableByteChannel 和 FileChannel的更多相关文章
- IO通道
本文原创,转载需标明原处. 通道,主要负责传输数据,相当于流,但流只能是输入或输出类型中的其一,而通道则可以兼并二者. 通道的基类是:Channel boolean isOpen() void clo ...
- JAVA NIO FileChannel 内存映射文件
文件通道总是阻塞式的. 文件通道不能创建,只能通过(RandomAccessFile.FileInputStream.FileOutputStream)getChannel()获得,具有与File ...
- JAVA NIO——Buffer和FileChannel
Java NIO和IO的主要区别 IO NIO 面向流 面向缓冲 阻塞IO 非阻塞IO 无 选择器 示例: import java.io.FileInputStream; import java.io ...
- java filechannel大文件的读写
java读取大文件 超大文件的几种方法 转自:http://wgslucky.blog.163.com/blog/static/97562532201332324639689/ java 读取一个 ...
- java的nio之:java的nio系列教程之FileChannel
一:Java NIO的FileChannel===>Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. ===>FileChannel无法设置为非 ...
- Java基础知识强化之IO流笔记78:NIO之 FileChannel
Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 1. 打开FileChannel 在 ...
- java中的拷贝文件FileChannel
以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码: 下边是传统的byte数组拷贝方法 </pre><pre name=&q ...
- Flume FileChannel优化(扩展)实践指南
本文系微博运维数据平台(DIP)在Flume方面的优化扩展经验总结,在使用Flume FileChannel的场景下将吞吐率由10M/s~20M/s提升至80M/s~90M/s,分为四个部分进行介绍: ...
- BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
从上篇文章中知道BufferedInputStream是自带缓冲区的输入流,可以大大减少IO次数,提供效率.下面的例子中实现了用BufferedInputStream与FileInputStream实 ...
随机推荐
- 表上的DELETE操作
在今天的文章里,我想给你快速展示下当我们从表里删除记录时,在SQL Server里发生了什么.首先我们来创建一个简单的表,在8KB的页上刚好能插入4条记录. -- Create a simple ta ...
- ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets
jqwidgets.js: 是一个功能完整的框架,它具有专业的可触摸的jQuery插件.主题.输入验证.拖放插件.数据适配器,内置WAI-ARIA(无障碍网页应用)可访问性.国际化和MVVM模式支持. ...
- SQL实现分组查询取前几条记录
我要实现的功能是统计订单日志表中每一个订单的前三条日志记录,表结构如下: 一个订单在定点杆日志表中有多条记录,要根据时间查询出每一个订单的前三条日志记录,sql如下: select b.OrderNu ...
- ASP.NET 运行时详解 揭开请求过程神秘面纱
对于ASP.NET开发,排在前五的话题离不开请求生命周期.像什么Cache.身份认证.Role管理.Routing映射,微软到底在请求过程中干了哪些隐秘的事,现在是时候揭晓了.抛开乌云见晴天,接下来就 ...
- Mantis 缺陷管理系统配置与安装
什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...
- 【jQuery基础学习】05 jQuery与Ajax以及序列化
好吧,这章不像上章那么水了,总是炒剩饭也不好. 关于AJAX 所谓Ajax,全名Asynchronous JavaScript and XML.(也就异步的JS和XML) 简单点来讲就是不刷新页面来发 ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换
[源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...
- HTML5实现屏幕手势解锁(转载)
来源:https://github.com/lvming6816077/H5lockhttp://threejs.org/examples/http://www.inf.usi.ch/phd/wett ...
- javascript 之注意url特殊字符限制
引子 浏览器URl地址,上网一定会用到,但是浏览器地址有中文或者浏览器url参数操作的时候,经常会用到encodeURIComponent()和decodeURIComponent()以及encode ...
- LinQ实战学习笔记(四) LINQ to Object, 常用查询操作符
这一篇介绍了下面的内容: 查询object数组 查询强类型数组 查询泛型字典 查询字符串 SelectMany 索引 Distinct操作符 排序 嵌套查询 分组 组连接 内连接 左外连接 交叉连接 ...