【Java NIO 简例】AsynchronousFileChannel

本文介绍了Java NIO的AsynchronousFileChannel类,展示了如何通过Future和CompletionHandler进行异步读写文件,包括从文件读取数据和向文件写入数据的实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.

原文:《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<Integer, ByteBuffer>() {  

  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<Integer, ByteBuffer>() {  

  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. });  

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值