Java NIO(十五)异步读写 AsynchronousFileChannel
在Java 7,AsynchronousFileChannel 被添加到了Java NIO中。使用AsynchronousFileChannel可以实现异步地读取和写入文件数据。
第一个参数是一个 PATH 的对像实例,它指向了那个与 AsynchronousFileChannel 相关联的文件。
第二个参数是一个或多个操作选项,它决定了 AsynchronousFileChannel 将对目标文件做何种操作。示例代码中我们使用了 StandardOpenOption.READ ,它表明我们将要对目标文件进行读操作。
读取数据
AsynchronousFileChannel 提供了两种读取数据的方式,都是调用它本身的 read() 方法。下面将对两种方式进行介绍。
一、第一种方法
调用 AsynchronousFileChannel 的 read() 方法,该方法反回一个 Future 类型的对象。
第一个参数是ByteBuffer,从 AsynchronousFileChannel 中读取的数据先写入这个 ByteBuffer 。
第二个参数表示从文件读取数据的开始位置。
此 read() 方法会立即返回,即使整个读的过程还没有完全结束。我们可以通过operation.isDone()来检查读取是否完成。这里的 operation 是上面调用 read() 方法返回的 Future 类型的实例。下面是一段详细的代码示例:
Path path = Paths.get("C:\\Users\\wahui\\Desktop\\听语\\test.txt");
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;
Future<Integer> operation = fileChannel.read(buffer, position);
TimeUnit.SECONDS.sleep(1);
if (operation.isDone()) {
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();
}
上面的程序首先创建了一个 AsynchronousFileChannel 对象,然后调用它的read()方法返回一个Future。然后通过调用isDone() 方法检测读取过程是否完成,完成后 isDone()方法将返回true。尽管这样让cpu空转了一会,
但是我们还是应该等读取操作完成后再进行后续的步骤。一旦读取完成,数据被存储到ByteBuffer,然后将数据转化为字符串既而输出。
一、第二种方法
第二种读取数据的方式是调用AsynchronousFileChannel 的另一个重载 read() 方法,改方法需要一个CompletionHandler 作为参数。下面是代码示例:
Path path = Paths.get("C:\\Users\\wa\\Desktop\\听语\\test.txt");
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate(1024);
fileChannel.read(buffer, 0, buffer, new CompletionHandler<>() {
@Override
public void completed(Integer result, ByteBuffer attachment) {
System.out.println("result = " + result);
attachment.flip();
byte[] data = new byte[attachment.limit()];
attachment.get(data);
System.out.println(new String(data));
attachment.clear();
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
}
});
TimeUnit.SECONDS.sleep(3);
当写入程序完成时,CompletionHandler的completed()方法将会被调用,相反的如果写入失败则会调用failed()方法。
要留意CompletionHandler的方法的参数 attachemnt是怎么使用的。

浙公网安备 33010602011771号