.
原文:《Java NIO AsynchronousFileChannel》
AsynchronousFileChannel 使得异步读写文件成为可能。此教程将解释如何使用该类。
创建一个 AsynchronousFileChannel
可通过 AsynchronousFileChannel.open() 方法创建实例:
Java代码
-
Path path = Paths.get("C:\\test\\file1.txt");
-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
读数据
可通过以下两种方式读取文件数据。两种方式都调用了 AsynchronousFileChannel.read() 方法。
通过 Future 读数据
read() 方法会立即返回一个 Future 对象,且不保证数据读取完成,所以需要有后续操作确保等到读取完成后再使用缓冲区中的数据。
Java代码
-
Future operation = fileChannel.read(buffer, 0);
此示例中等待读取完成的方式并未充分利用CPU资源,它仅用于说明需要等数据读取完成后再操作缓存区:
Java代码
-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
-
ByteBuffer buffer = ByteBuffer.allocate(1024);
-
-
// 从文件起始处开始读,所以 position 为 0
-
Future operation = fileChannel.read(buffer, 0);
-
-
while(!operation.isDone()) {
-
}
-
-
buffer.flip();
-
byte[] data = new byte[buffer.limit()];
-
buffer.get(data);
-
// data 就是读到的数据
通过 CompletionHandler 读数据
例:
Java代码
-
// 从文件起始处开始读,所以 position 为 0
-
fileChannel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
-
@Override
-
public void completed(Integer result, ByteBuffer attachment) {
-
System.out.println("read byte count: " + result);
-
attachment.flip();
-
byte[] data = new byte[attachment.limit()];
-
attachment.get(data);
-
// data 就是读到的数据
-
}
-
-
@Override
-
public void failed(Throwable exc, ByteBuffer attachment) {
-
}
-
});
该示例中,将 buffer 作为 CompletionHandler 的 “附件(attachment)” 只是为了操作方便,你可以选择其它对象(下同)。
读取操作失败时会调用 failed() 方法,而非 completed()(下同)。
写数据
类似的,写数据也有两种方式,都调用 AsynchronousFileChannel.write() 方法
通过 Future 写数据
例:
Java代码
-
// byte[] data = ...
-
-
Path path = Paths.get("C:\\test\\file1.txt");
-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
-
ByteBuffer buffer = ByteBuffer.allocate(1024);
-
buffer.put(data);
-
buffer.flip();
-
-
// 从文件起始处开始写,所以 position 为 0
-
Future operation = fileChannel.write(buffer, 0);
-
-
while (!operation.isDone()) {
-
// 等待写操作完成
-
}
StandardOpenOption.CREATE 是为了确保文件存在(下同)。
通过 CompletionHandler 写数据
例:
Java代码
-
// byte[] data = ...
-
-
Path path = Paths.get("C:\\test\\file1.txt");
-
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
-
ByteBuffer buffer = ByteBuffer.allocate(1024);
-
buffer.put(data);
-
buffer.flip();
-
-
// 从文件起始处开始写,所以 position 为 0
-
fileChannel.write(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
-
@Override
-
public void completed(Integer result, ByteBuffer attachment) {
-
System.out.println("written byte count: " + result);
-
}
-
-
@Override
-
public void failed(Throwable exc, ByteBuffer attachement) {
-
}
-
});