Answering the "usefulness" part of the question:
One rather subtle gotcha of using FileChannel
over FileOutputStream
is that performing any of its blocking operations (e.g. read()
or write()
) from a thread that's in interrupted state will cause the channel to close abruptly with java.nio.channels.ClosedByInterruptException
.
Now, this could be a good thing if whatever the FileChannel
was used for is part of the thread's main function, and design took this into account.
But it could also be pesky if used by some auxiliary feature such as a logging function. For example, you can find your logging output suddenly closed if the logging function happens to be called by a thread that's also interrupted.
It's unfortunate this is so subtle because not accounting for this can lead to bugs that affect write integrity.[1][2]