Don’t get me wrong, the check itself is almost correct.
Files.exists is an evil method. Not because of the performance issue mentioned on Java 8 (since fixed), but because it silently returns
false if it gets an exception.
Files.isDirectory, and anything along the same lines are similarly guilty. The correct course of action is actually to call
Files.readAttributes, which properly throws the exception.
What I had hoped to draw attention to is that in avoiding calling one bad thing, the recommended course of action was in fact to call two bad things:
Path#toFile() breaks abstraction and should be banned unless the code is shown to properly handle the
File#exists() is evil on the same basis, that it does not throw
IOException, and thus should be avoided for the same reason as
Ah, yes. And of course, I almost forgot. A footnote that file existence checks in themselves are inherently race conditions, in that the information is out of date as soon as you get it.