【Java NIO 简例】AsynchronousFileChannel

.

原文:《Java NIO AsynchronousFileChannel》

AsynchronousFileChannel 使得异步读写文件成为可能。此教程将解释如何使用该类。

 

创建一个 AsynchronousFileChannel

可通过 AsynchronousFileChannel.open() 方法创建实例:

Java代码

 

  1. Path path = Paths.get("C:\\test\\file1.txt");  

  2. AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);  

 

读数据

可通过以下两种方式读取文件数据。两种方式都调用了 AsynchronousFileChannel.read() 方法。

通过 Future 读数据

read() 方法会立即返回一个 Future 对象,且不保证数据读取完成,所以需要有后续操作确保等到读取完成后再使用缓冲区中的数据。

Java代码

 

  1. Future operation = fileChannel.read(buffer, 0);  

 

此示例中等待读取完成的方式并未充分利用CPU资源,它仅用于说明需要等数据读取完成后再操作缓存区:

Java代码

 

  1. AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);  

  2. ByteBuffer buffer = ByteBuffer.allocate(1024);  

  3.   

  4. // 从文件起始处开始读,所以 position 为 0  

  5. Future operation = fileChannel.read(buffer, 0);  

  6.   

  7. while(!operation.isDone()) {  

  8. }  

  9.   

  10. buffer.flip();  

  11. byte[] data = new byte[buffer.limit()];  

  12. buffer.get(data);  

  13. // data 就是读到的数据  

 

通过 CompletionHandler 读数据

例:

Java代码

 

  1. // 从文件起始处开始读,所以 position 为 0  

  2. fileChannel.read(buffer, 0, buffer, new CompletionHandler() {  

  3.   @Override  

  4.   public void completed(Integer result, ByteBuffer attachment) {  

  5.     System.out.println("read byte count: " + result);  

  6.     attachment.flip();  

  7.     byte[] data = new byte[attachment.limit()];  

  8.     attachment.get(data);  

  9.     // data 就是读到的数据  

  10.   }  

  11.   

  12.   @Override  

  13.   public void failed(Throwable exc, ByteBuffer attachment) {  

  14.   }  

  15. });  

该示例中,将 buffer 作为 CompletionHandler 的 “附件(attachment)” 只是为了操作方便,你可以选择其它对象(下同)。

读取操作失败时会调用 failed() 方法,而非 completed()(下同)。

 

写数据

类似的,写数据也有两种方式,都调用 AsynchronousFileChannel.write() 方法

通过 Future 写数据

例:

Java代码

 

  1. // byte[] data = ...  

  2.   

  3. Path path = Paths.get("C:\\test\\file1.txt");  

  4. AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);  

  5. ByteBuffer buffer = ByteBuffer.allocate(1024);  

  6. buffer.put(data);  

  7. buffer.flip();  

  8.   

  9. // 从文件起始处开始写,所以 position 为 0  

  10. Future operation = fileChannel.write(buffer, 0);  

  11.   

  12. while (!operation.isDone()) {  

  13.   // 等待写操作完成  

  14. }  

StandardOpenOption.CREATE 是为了确保文件存在(下同)。

 

通过 CompletionHandler 写数据

例:

Java代码

 

  1. // byte[] data = ...  

  2.   

  3. Path path = Paths.get("C:\\test\\file1.txt");  

  4. AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);  

  5. ByteBuffer buffer = ByteBuffer.allocate(1024);  

  6. buffer.put(data);  

  7. buffer.flip();  

  8.   

  9. // 从文件起始处开始写,所以 position 为 0  

  10. fileChannel.write(buffer, 0, buffer, new CompletionHandler() {  

  11.   @Override  

  12.   public void completed(Integer result, ByteBuffer attachment) {  

  13.     System.out.println("written byte count: " + result);  

  14.   }  

  15.   

  16.   @Override  

  17.   public void failed(Throwable exc, ByteBuffer attachement) {  

  18.   }  

  19. });  

 

 

你可能感兴趣的:(Java,IO)