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. Django--models表操作

    需求 models对表的增删改查 知识点 1.基础操作 1.1  增 方法一 1 models.Tb1.objects.create(c1='xx', c2='oo')  #增加一条数据 1 2 di ...

  2. SQL Server 诊断查询-(1)

    Query #1 is Version Info. SQL and OS Version information for current instance SELECT @@SERVERNAME AS ...

  3. C#如何获取CPU处理器核心数量

    有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核心数量和逻辑处理器数量,这些可以不同.两颗双核超线程(启用)处理器的机器情况下有:2个物理处理器.4个核心和8个逻辑处理器. 逻辑处理器 ...

  4. c#使用aspose.cells 从datatable导出数据到excel

    string json=value.Value; DataTable dt=Utils.JsonDataTableConvert.ToDataTable(json); string fileName ...

  5. 根据数据库输出XML菜单

    USE [test_YTHH] GO /****** Object:  StoredProcedure [dbo].[usp_Print_SCC_Menu]    Script Date: 04/08 ...

  6. 011_URL和Ajax辅助器方法

    创建基本的链接和URL 在我们介绍链接或URL之前先做一些准备,我们这部分要介绍的知识将要使用的项目就是之前建立的HelperMethods项目,现在需要先为其添加一个People控制器,并在其中定义 ...

  7. 004_URL 路由 - URL 路由

    在Web Form 情况下,每一个 ASPX页面既是一个文件,又是一个队请求自包含的响应.而在 MVC 情况下,请求是由控制器类中的动作方法处理的,而且与硬盘上的文件没有一对一的相互关系. ASP.N ...

  8. jQuery $.extend() 和 $.fn.extend() 用法

    http://blog.csdn.net/xuemoyao/article/details/19021659

  9. ahjesus 获取当前方法被调用执行的具体位置,包括命名空间和方法

    MethodBase method = ).GetMethod(); string ahjesus = method.ReflectedType.FullName + "." + ...

  10. 【iOS】通知监听

    下例为:监听文本框  accountField  内容的改变, 当发生改变时, 调用textChange方法(多次).监听结束需要移除通知. - (void)viewDidLoad { [super ...