Pathpath= Paths.get("README.md"); // this will create asynchronous enabled channel // other channel like Socket also has this feature AsynchronousFileChannelc= AsynchronousFileChannel.open(path, StandardOpenOption.READ);
1 2 3 4 5
// we read and write data like this, but they return Future object instead Future<Integer> operation = fileChannel.read(buffer, 0); Future<Integer> operation = fileChannel.write(buffer, position); // then we poke around to see if this operation is completed while(!operation.isDone());
Same usage as comparator, simply use CompletionHandler to deal with read and write operation. Upon read/write completion, the completed method will be invoked.