1.5.6、文件异步AIO
@Slf4j
public class AIOTest {
public static void main(String[] args) {
try {
AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("a.txt"));
ByteBuffer buffer = ByteBuffer.allocate(128);
//buffer 开始位置 附件 回调方法
log.debug("read begin..");
channel.read(buffer, 0, buffer, new CompletionHandler<Integer, ByteBuffer>() {
//result:读到的字节数
//attachment:附件
@Override//成功回调
public void completed(Integer result, ByteBuffer attachment) {
log.debug("read succ..");
/* attachment.flip();
ByteBufferUtil.debugAll(attachment);*/
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) {
exc.printStackTrace();
}
});
log.debug("read end..");
System.in.read();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
标签:12,NIO,read,buffer,AIO,attachment,ByteBuffer,new,public
From: https://www.cnblogs.com/jpymll/p/16784278.html