Example usage for java.nio.channels AsynchronousFileChannel read

List of usage examples for java.nio.channels AsynchronousFileChannel read

Introduction

In this page you can find the example usage for java.nio.channels AsynchronousFileChannel read.

Prototype

public abstract Future<Integer> read(ByteBuffer dst, long position);

Source Link

Document

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path file = Paths.get("/usr/a.txt");
    AsynchronousFileChannel channel = AsynchronousFileChannel.open(file);

    ByteBuffer buffer = ByteBuffer.allocate(100_000);
    Future<Integer> result = channel.read(buffer, 0);

    while (!result.isDone()) {
        ProfitCalculator.calculateTax();
    }/*from   w ww.  ja  va  2s  .c o  m*/
    Integer bytesRead = result.get();
    System.out.println("Bytes read [" + bytesRead + "]");
}