So I came up with the following function based on the docs and examples I found online, to write files in async way:
public static Future<Integer> createAndWriteToFile(String fullFileName, String content) throws IOException {
Path file = Utils.createFile(fullFileName);
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
ByteBuffer buffer = ByteBuffer.wrap(content.getBytes());
Future<Integer> operation = fileChannel.write(buffer, 0);
buffer.clear();
return operation;
}
However, there is absolutely no documentation on what to expect from calling operation.get().intValue()
!
In debug mode, successful file creation/writing returns Integer value 23.
What are other possible values?
Docs:
https://docs.oracle.com/javase/8/docs/api/java/nio/channels/AsynchronousFileChannel.html#write-java.nio.ByteBuffer-long-
And here: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#get--
Returns: the computed result
Helpful, its not.
As an aside question - it surprised me, Java is considered one of the most well documented languages, how comes this function is not documented?