java8 AIO AsynchronousFileChannel例

本文介绍了一个使用Java NIO的AsynchronousFileChannel进行文件异步读写的示例。通过两个不同的方法演示了如何利用Future和CompletionHandler来处理读取操作,并展示了如何将读取的数据写回到另一个文件中。

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

package com.kd.nio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;


public class AsynchronousFileChannelTest {
	public static ByteBuffer read()throws Exception{
		Path path = Paths.get("f:","test1.txt");
  		AsynchronousFileChannel afc = AsynchronousFileChannel.open(path , StandardOpenOption.READ) ;
  		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
  		
  		
  		Future<Integer> read = afc.read(byteBuffer, 0);
  		
  		while(!read.isDone()){
  			System.out.println("reading");
  		}
  		System.out.println(read.isDone());
  		
  		System.out.println(byteBuffer);
  		afc.close();
  		return byteBuffer;
	}
	
	public static void read2()throws Exception{
		Path path = Paths.get("f:","test1.txt");
  		AsynchronousFileChannel afc = AsynchronousFileChannel.open(path , StandardOpenOption.READ) ;
  		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
  		
  		
  		 afc.read(byteBuffer, 0, "at", new CompletionHandler<Integer,Object>(){

			@Override
			public void completed(Integer result, Object attachment) {
				System.out.println(attachment+"/"+result);
				System.out.println(byteBuffer);
				try {
					write2(byteBuffer);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void failed(Throwable exc, Object attachment) {
				
			}
  			 
  		 });
  		
  		Thread.sleep(5000);
  		System.out.println("close");
  		afc.close();
	}
	
	public static void write(ByteBuffer byteBuffer)throws Exception{
		Path path = Paths.get("f:","test.txt");
  		AsynchronousFileChannel afc = AsynchronousFileChannel.open(path , StandardOpenOption.CREATE,StandardOpenOption.WRITE) ;
  		byteBuffer.flip();
  		Future<Integer> write = afc.write(byteBuffer, 0);
  		while(!write.isDone()){
  			System.out.println("writing");
  		}
  		System.out.println(write.isDone());
  		System.out.println(byteBuffer);
  		afc.close();
	}
	
	public static void write2(ByteBuffer byteBuffer)throws Exception{
		Path path = Paths.get("f:","test.txt");
  		AsynchronousFileChannel afc = AsynchronousFileChannel.open(path , StandardOpenOption.CREATE,StandardOpenOption.WRITE) ;
  		byteBuffer.flip();
  		afc.write(byteBuffer, 0, "attachment",  new CompletionHandler<Integer,Object>(){

			@Override
			public void completed(Integer result, Object attachment) {
				System.out.println(result+"/"+attachment);
				System.out.println(byteBuffer);
			}

			@Override
			public void failed(Throwable exc, Object attachment) {
				System.out.println(byteBuffer);
			}
  			
  		});
  		
  		Thread.sleep(5000);
  		System.out.println("close");
  		afc.close();
  	
  		
	}
     
      public static void main(String[] args) throws Exception{
    	
    	 read2();
  		
	}
	

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值