Java NIO AsynchronousFileChannel

Java NIO AsynchronousFileChannel

在Java 7,AsynchronousFileChannel被添加到了Java NIO中。使用AsynchronousFileChannel可以实现异步地读取和写入文件数据。

1.创建一个AsynchronousFileChannel

和NIO包中的许多类一样,我们用一个静态open()方法来打开一个AsynchronousFileChannel。

Path path = Paths.get("data/test.xml");
AsynchronousFileChannel fileChannel =
    AsynchronousFileChannel.open(path, StandardOpenOption.READ);

第一个参数是一个Path,指向与AsynchronousFileChannel相关联的文件。

第二个参数是一个或多个操作选项,他决定了AsynchronousFileChannel对目标文件做怎样的操作。示例代码中我们使用AsynchronousFileChannel.READ,表明我们要进行读操作。

2.读取数据

AsynchronousFileChannel使用read()方法来读取数据,但有两种方法。

第一种方式是调用AsynchronousFileChannel的read()方法,返回一个Future类型的对象

Future<Integer> operation = fileChannel.read(buffer, 0)
  • 第一个参数使用了一个ByteBuffer类型的变量,从AsynchronousFileChannel中读入的数据写进ByteBuffer。

  • 第二个参数指示了从文件字节序的什么位置开始读取。

无论读操作是否完成,read()方法立刻返回,后续可以用FutureisDone()方法来检查读操作是否完成。

下面是一个使用示例

// open一个AsynchronousFileChannel
AsynchronousFileChannel fileChannel = 
    AsynchronousFileChannel.open(path, StandardOpenOption.READ);

// 创建buffer和position
ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;

// 调用read方法,返回Future对象
Future<Integer> operation = fileChannel.read(buffer, position);

// 循环,直到read完成为止
while(!operation.isDone());

// 读数据到一个字节数组data,并打印
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
System.out.println(new String(data));
buffer.clear();

使用方法在上面的示例中写的很清楚了。

第二种方法是在read方法参数中使用CompletionHandler读取数据

下面是一个使用示例

fileChannel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {
    @Override
    public void completed(Integer result, ByteBuffer attachment) {
        System.out.println("result = " + result);

        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) {

    }
});

如上,我们将一个CompletionHandler作为read方法的参数,并重写了completed方法。当read操作完成后,这个completed方法会被调用。

  • result参数会是read中读取的字节数。
  • attachment参数是一个ByteBuffer,存了读取的数据。它其实就是read方法的第三个参数,这里我们使用了ByteBuffer类型,其实别的啥也可以,反正completed()是自己写。

读取失败时,failed()方法会被调用。

3.写入数据

就像读取一样,我们同样有两种方式向 AsynchronousFileChannel 写入数据。我们可以调用它的2个重载的 write() 方法。下面我们将分别加以介绍。

使用Future写入数据

AsynchronousFileChannel也可以异步写入数据,下面是一个完整的示例。

Path path = Paths.get("data/test-write.txt");
AsynchronousFileChannel fileChannel = 
    AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);

ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;

buffer.put("test data".getBytes());
buffer.flip();

// 返回一个Future
Future<Integer> operation = fileChannel.write(buffer, position);
buffer.clear();

while(!operation.isDone());

System.out.println("Write done");

代码很好看懂,这里就不做解释了。

注意写入的目标文件需要提前准备好,否则会抛出java.nio.file.NoSuchFileException,我们可以用下面的代码解决这个问题。

if(!File.exists(path)){
    Files.createFile(path);
}

使用CompletionHandler写入数据

我们也可以使用 CompletionHandler代替Future向AsynchronousFileChannel写入数据,这种方式可以更加直接的知道写入过程是否完成。下面是示例程序

Path path = Paths.get("data/test-write.txt");
if(!Files.exists(path)){
    Files.createFile(path);
}
AsynchronousFileChannel fileChannel = 
    AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);

ByteBuffer buffer = ByteBuffer.allocate(1024);
long position = 0;

buffer.put("test data".getBytes());
buffer.flip();

fileChannel.write(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {

    @Override
    public void completed(Integer result, ByteBuffer attachment) {
        System.out.println("bytes written: " + result);
    }

    @Override
    public void failed(Throwable exc, ByteBuffer attachment) {
        System.out.println("Write failed");
        exc.printStackTrace();
    }
});

当写入程序完成时,CompletionHandler的completed()方法将会被调用,相反的如果写入失败则会调用*failed()*方法。

要留意CompletionHandler的方法的参数 attachemnt是怎么使用的,其实和read中的类似。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值