Search code examples
apijava-iojava

Why both Files.exists(…) and Files.notExists(…)?


IMO having both of these methods is redundant and really unclear! i can't quite understand why both of these methods are designed in Files API ??

Files.exist(..) returns true if file really exist and false if not exist or not having permission. so why in the hell there is a Files.notExist(..) ??

Oracle Docs says !Files.exists(…) is not equivalent to Files.notExists(…)!? maybe there is something that i couldn't understand well!!

what is benefit of using notExist() when there is an exist() method?


Solution

  • I think the javadoc is pretty clear why notExists is not a logical complement of the exists method. Logical complement B = !A means that if A is true, B is false and vice versa. This is not the case here as both methods may return false at the same time.

    "Where it is not possible to determine if a file exists or not then both methods return false."

    Files.exists JavaDoc

    Files.notExists JavaDoc