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 的转换,也就是字节到字符的转换,需要使用对应的编码的解码器进行解码。

 

转载于:https://www.cnblogs.com/digdeep/p/4481434.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值