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实 ...
随机推荐
- 在SQL Server里如何进行页级别的恢复
在今天的文章里我想谈下每个DBA应该知道的一个重要话题:在SQL Server里如何进行页级别还原操作.假设在SQL Server里你有一个损坏的页,你要从最近的数据库备份只还原有问题的页,而不是还原 ...
- Javascript之旅(一)
Javascript之旅(一) 一.基础知识 基本语法 变量 数据类型 字符串 数组 对象 条件判断 循环 Map和Set iterable 为什么要学习JavaScript JavaScript 是 ...
- ASP.NET 文件上传类 简单好用
调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...
- UWP开发入门(十三)——用Diagnostic Tool检查内存泄漏
因为.NET的垃圾回收机制相当完善,通常情况下我们是不需要关心内存泄漏的.问题人一但傻起来,连自己都会害怕,几个页面跳啊跳的,内存蹭蹭的往上涨,拉都拉不住.这种时候我们就需要冷静下来,泡一杯热巧克力. ...
- Scrum1.2--spring计划
项目功能--深入分析 燃尽图
- Qt Style Sheet实践(三):QCheckBox和QRadioButton
导读 单选按钮(QRadioButton)和复选框(QCheckBox)是界面设计中的重要元素.单选按钮只允许用户在一组选项中选择一个,且当其中一个被选中的时候,按钮组中的其他单选按钮自动取消.复选框 ...
- JAVA - 大数类详解
写在前面 对于ACMer来说,java语言最大的优势就是BigInteger,Bigdecimal,String三个类. 这三个类分别是高精度整数,高精度浮点数和字符串,之所以说这个是它的优势是因为j ...
- 使用HttpRequester模拟发送及接收Json请求
1.开发人员在火狐浏览器里经常使用的工具有Firebug,httprequester,restclient......火狐浏览器有一些强大的插件供开发人员使用!需要的可以在附加组件中扩展. 2.htt ...
- Diy页面服务端渲染解决方案
1. 问题由来 在移动互联网电商领域,运营每天需要搭建多个促销页面来吸引用户去点击去购买,一开始程序员临时写个新页面去实现,可这些页面可以用几次就不用了,每次创建新页面去实现费时费力,而且,电商的运营 ...
- [CLR via C#]21. 自动内存管理(垃圾回收机制)
目录 理解垃圾回收平台的基本工作原理 垃圾回收算法 垃圾回收与调试 使用终结操作来释放本地资源 对托管资源使用终结操作 是什么导致Finalize方法被调用 终结操作揭秘 Dispose模式:强制对象 ...