This post shows how to read file in Java asynchronously using the java.nio.channels.AsynchronousFileChannel class. Using AsynchronousFileChannel you can create an asynchronous file channel for reading, writing, and manipulating a file. To see how to write a file asynchronously in Java, check this post- Write a File Asynchronously in Java Reading file using AsynchronousFileChannel For reading a file there are two read methods- One of the read() method returns a Future instance representing the result of an asynchronous computation. In another read() method CompletionHandler instance is passed as an argument which consumes the result of an asynchronous I/O operation. 1. Java program to read file asynchronously First let’s see a program which uses the read method that returns Future instance. Future<Integer> read(ByteBuffer bufffer, long position) - Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. im
Java, Spring, Web development tutorials with examples