exists and notExists
Alan.Bateman at Sun.COM
Tue May 27 00:28:31 PDT 2008
Elliotte Harold wrote:
> As I read the JavaDoc, the only reason the exists() method won't be
> able to say is if it throws a SecurityException. Correct?
This is nothing to do with security managers and SecurityExceptions. The
Java virtual machine may not have access to the file or one of the
directories in the path. In that case, it is not possible to say if the
file exists or not. There can be many other I/O error related reasons too.
> (If not, the JavaDoc needs to be clarified.)
If the javadoc isn't clear then we can improve it. In this case it says
it returns false "if the file does not exist or its existence cannot be
> I don't feel like notExists is convenient enough to justify its
> existence. Presumably if one cannot tell if a file exists, one cannot
> tell if it doesn't exist either. Both are SecurityExceptions.
If we can't determine the file exists then the exists() method will
return false. You cannot interpret this to mean the file does not exist.
This is the reason for the notExists method. It's a trivial convenience
method to allow you take action when it is confirmed that a file does
> Can you imagine the bloat if every isXXX() method in Java was matched
> by a isNotXXX() method? Let's not start down this path. A single
> exists() method is fully sufficient.
The existence check differs from the other access checks and a boolean
result is insufficient. There is no plan to add complement variants of
the other methods.
More information about the nio-discuss