ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java NIO AsynchronousFileChannel

2021-08-07 22:02:16  阅读:183  来源: 互联网

标签:Java NIO buffer read AsynchronousFileChannel ByteBuffer path data


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中的类似。

标签:Java,NIO,buffer,read,AsynchronousFileChannel,ByteBuffer,path,data
来源: https://www.cnblogs.com/WangXianSCU/p/15113448.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有