六、Nio之FileChannel

Java NIO 中的 FileChannel 是一个连接到文件的通道。可以通过文件通道读写文件。

FileChannel 无法设置为非阻塞模式,它总是运行在阻塞模式下。

打开 FileChannel:

在使用FileChannel之前, 必须先打开它。 但是, 我们无法直接打开一个FileChannel, 需要通过使用一个InputStream、

OutputStream 或 RandomAccessFile 来获取一个 FileChannel 实例。下面是通过 RandomAccessFile 打开

FileChannel 的示例:

 
 
  1. RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
  2. FileChannel inChannel = aFile.getChannel();

从 FileChannel  读取数据:

调用多个 read()方法之一从 FileChannel 中读取数据。如:

 
 
  1. ByteBuffer buf = ByteBuffer.allocate(48);
  2. int bytesRead = inChannel.read(buf);

首先,分配一个 Buffer。从 FileChannel 中读取的数据将被读到 Buffer 中。

然后,调用 FileChannel.read()方法。该方法将数据从 FileChannel 读取到 Buffer 中。read()方法返回的 int 值表示了有多少字节被读到了 Buffer 中。如果返回-1,表示到了文件末尾。

向 FileChannel 写数据:

使用 FileChannel.write()方法向 FileChannel 写数据,该方法的参数是一个 Buffer。如:

 
 
  1. String newData = "New String to write to file..." + System.currentTimeMillis();
  2. ByteBuffer buf = ByteBuffer.allocate(48);
  3. buf.clear();
  4. buf.put(newData.getBytes());
  5. buf.flip();
  6. while(buf.hasRemaining()) {
  7. channel.write(buf);
  8. }

注意 FileChannel.write()是在 while 循环中调用的。因为无法保证 write()方法一次能向 FileChannel 写入多少字节,因此需要重复调用 write()方法,直到 Buffer 中已经没有尚未写入通道的字节。

关闭 FileChannel:

用完 FileChannel 后必须将其关闭。如:

 
 
  1. channel.close();

FileChannel 的 的 position 方法:

有时可能需要在 FileChannel 的某个特定位置进行数据的读/写操作。

可以通过调用 position()方法获取 FileChannel的当前位置。

也可以通过调用 position(long pos)方法设置 FileChannel 的当前位置。

这里有两个例子:

 
 
  1. long pos = channel.position();
  2. channel.position(pos +123);

如果将位置设置在文件结束符之后,然后试图从文件通道中读取数据,读方法将返回-1 —— 文件结束标志。

如果将位置设置在文件结束符之后,然后向通道中写数据,文件将撑大到当前位置并写入数据。这可能导致“文件空

洞”,磁盘上物理文件中写入的数据间有空隙。

FileChannel 的 的 size  方法:

FileChannel 实例的 size()方法将返回该实例所关联文件的大小。如:

 
 
  1. long fileSize = channel.size();

FileChannel 的 的 truncate  方法:

可以使用 FileChannel.truncate()方法截取一个文件。截取文件时,文件将中指定长度后面的部分将被删除。如:

 
 
  1. channel.truncate(1024);

这个例子截取文件的前 1024 个字节。

FileChannel 的 的 force  方法:

FileChannel.force()方法将通道里尚未写入磁盘的数据强制写到磁盘上。出于性能方面的考虑,操作系统会将数据

缓存在内存中, 所以无法保证写入到 FileChannel 里的数据一定会即时写到磁盘上。 要保证这一点, 需要调用 force()

方法。

force()方法有一个 boolean 类型的参数,指明是否同时将文件元数据(权限信息等)写到磁盘上。

下面的例子同时将文件数据和元数据强制写到磁盘上:

 
 
  1. channel.force(true);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值