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的更多相关文章

  1. IO通道

    本文原创,转载需标明原处. 通道,主要负责传输数据,相当于流,但流只能是输入或输出类型中的其一,而通道则可以兼并二者. 通道的基类是:Channel boolean isOpen() void clo ...

  2. JAVA NIO FileChannel 内存映射文件

      文件通道总是阻塞式的. 文件通道不能创建,只能通过(RandomAccessFile.FileInputStream.FileOutputStream)getChannel()获得,具有与File ...

  3. JAVA NIO——Buffer和FileChannel

    Java NIO和IO的主要区别 IO NIO 面向流 面向缓冲 阻塞IO 非阻塞IO 无 选择器 示例: import java.io.FileInputStream; import java.io ...

  4. java filechannel大文件的读写

    java读取大文件 超大文件的几种方法 转自:http://wgslucky.blog.163.com/blog/static/97562532201332324639689/   java 读取一个 ...

  5. java的nio之:java的nio系列教程之FileChannel

    一:Java NIO的FileChannel===>Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. ===>FileChannel无法设置为非 ...

  6. Java基础知识强化之IO流笔记78:NIO之 FileChannel

    Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 1. 打开FileChannel 在 ...

  7. java中的拷贝文件FileChannel

    以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码: 下边是传统的byte数组拷贝方法 </pre><pre name=&q ...

  8. Flume FileChannel优化(扩展)实践指南

    本文系微博运维数据平台(DIP)在Flume方面的优化扩展经验总结,在使用Flume FileChannel的场景下将吞吐率由10M/s~20M/s提升至80M/s~90M/s,分为四个部分进行介绍: ...

  9. BufferedInputStream,FileInputStream,FileChannel实现文件拷贝

    从上篇文章中知道BufferedInputStream是自带缓冲区的输入流,可以大大减少IO次数,提供效率.下面的例子中实现了用BufferedInputStream与FileInputStream实 ...

随机推荐

  1. SQL Server技术问题之游标优缺点

    分类: MS SQL SERVER 支持三种类型的游标:Transact_SQL 游标,API 服务器游标和客户游标. (1) Transact_SQL 游标 Transact_SQL 游标是由DEC ...

  2. 餐厅到店点餐系统(APP)

    MY-HR 成员: 角色分配 学号 博客园 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 郭明茵 用户 201406114204 ...

  3. 【循序渐进学Python】7.面向对象的核心——类型(上)

    我们知道Python是一门面向对象的脚本语言.从C#的角度来看:首先Python支持多继承.Python 类型成员通常都是public的,并且所有成员函数都是virtual的(可以直接重写). 1. ...

  4. 常用库nuget包集合

    ColorConsole htmlagilitypack.1.4.9.5 经测试效率比 CsQueryLaster 高 csvhelper Extend Devlib系列一套 itextsharp l ...

  5. HDU 1069---背包---Monkey and Banana

    HDU 1069 Description A group of researchers are designing an experiment to test the IQ of a monkey. ...

  6. [小北De编程手记] : Lesson 01 - Selenium For C# 之 环境搭建

    在我看来一个自动化测试平台的构建,是一种很好的了解开发语言,单元测试框架,自动化测试驱动,设计模式等等等的途径.因此,在下选择了自动化测试的这个话题来和大家分享一下本人关于软件开发和自动化测试的认识. ...

  7. Echarts图表控件使用总结1(Line,Bar)

    问题篇(详解):http://www.cnblogs.com/hanyinglong/p/4708337.html 1.前言 a.在系统开发过程中可能会使用到图表控件,一个好的图标控件可以使我们的网站 ...

  8. [原创]winform_PC宴会图片抽奖/文字抽奖

    14年6月 好友结婚 14年4月左右知道他们婚礼由迎宾照抽奖的环节 问我有没有可以用的抽奖软件 我网上找了一会儿,就放弃了,自己做一个更快不是? 14年6月,PC宴会图片抽奖软件成功使用 --- 操作 ...

  9. css三角形的实现

    实底三角形: <html> <head> <title></title> <style type="text/css"> ...

  10. 显示所有SAP图标的ABAP代码

    TABLES: icon. INCLUDE <icon>. FIELD-SYMBOLS: <f>. SELECT * FROM icon. ASSIGN (icon-name) ...