Why both Files.exists(…) and Files.notExists(…)?
Asked Answered
K

4

8

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?

Knoll answered 24/1, 2014 at 14:12 Comment(10)
Which package are you working in? java.io.* does not have a File class with a notExists() method.Felt
java.nio.file.Files -- JDK7Knoll
It's Files, not File.Lebanon
The link you provide says it right there: If both exists and notExists return false, the existence of the file cannot be verified..Ricciardi
Read the rest of those docs you linked. "When you are testing a file's existence, three results are possible: The file is verified to exist. The file is verified to not exist. The file's status is unknown. This result can occur when the program does not have access to the file. If both exists and notExists return false, the existence of the file cannot be verified."Latent
@Ricciardi its about UnKnown state ( security issues ) both of methods return false if they couldn't access the file -- so again having boh method is seems redundant!!Knoll
@Latent i read entire article! what you made bold is about seurity permissions -- both method return false it may happen your programm has't permission to access file! so again both method isn't necessary you can check that with exist()Knoll
@MortezaAdi: They are definitely not redundant. A file doesn't have to exist just because notExists() returns false, and vice versa.Ricciardi
@Ricciardi if exist() would return true in case file exist and false if doesn't and throw security exception IMHO could be much more clear and easy to understand than current design.Knoll
I agree that the javadocs are very unclear, even though they may be perfectly clear to some.Lancewood
L
6

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

Lebanon answered 24/1, 2014 at 14:16 Comment(5)
Sure i don't argue that what i'm saying is notExist() is unneccassary! what can you achive with notExist() when there is an exist() method ??Knoll
Note that the result of both methods are immediately outdated. So they give you a check (valid of the moment of the call) is a file exists/not exists. Respectively, this is what you can achieve with them. If you had no notExists, you would call exists and if it returns false, you would conclude the file does not exist. But as the JavaDoc says, this conclusion is incorrect (as notExists may be returning also false at the same time). Yes, it is a bit confusing :) OK.Lebanon
if exist() would return true in case file exist and false if doesn't and throw security exception IMHO could be much more clear and easy to understand than current design.Knoll
OK everyone can have an opinion. I am not defending any decisions design here. Just trying to answer your question.Lebanon
Thanks buddy! my primary question was about the design. That was my mistake to putting two different question in one :). maybe because i couldn't understand the intention of methods. now it sound all clear to me :)Knoll
S
2

You provided the answer already in the link.

    The file is verified to exist.
    The file is verified to not exist.
    The file's status is unknown. This result can occur when the program does not have access to the file.

If both exists and notExists return false, the existence of the file cannot be verified.

That means if exists() or notExists() return true you can be sure of the result, if the return false it can mean that the state could not be determined. So use the appropriate method if you want to check for existence or non existence.

Swallowtail answered 24/1, 2014 at 14:17 Comment(4)
Sure i don't argue that what i'm saying is notExist() is unneccassary! what can you achieve with notExist() when there is an exist() method ??Knoll
Since exists()==false does not prove the non existence of a file, you can use notExists() for the cases when you want to check if the file does not exists, e.g., before creating a new file.Deformation
i see know! however still not clear why API designed this way? if exist() would return true in case file exist and false if doesn't and throw security exception IMHO could be much more clear and easy to understand than current design.Knoll
They wanted to avoid throwing IOException if the method could not perform its check so they decided to have two methods instead to aleviate the ambiguity of the false result.Deformation
M
1

Let's say that you want to access an existing file at some location. If you want to be sure that the access doesn't fail due to the file's non-existence, you call exists( path,... ) and proceed only if this returns true.

Let's say that you want to create a new file at some location. If you want to be sure that the create doesn't fail due to the file's existence, you call notExists( path,... ) and proceed only if this returns true.

Note that the negation of exists to handle the second case is not a guarantee in the same way notExists() delivers. And vice versa for ! notExists in the first scenario.

Moskowitz answered 24/1, 2014 at 14:34 Comment(0)
P
1

Files.exists(...) and Files.notExists(...) are not redundant but gives different information as can be seen in below code

 LinkOption linkOptions[] = new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
    Path filePath = Paths.get("application.properties");
    if (Files.exists(filePath, linkOptions))
    {
      System.out.println("file exists!");
    } else if (Files.notExists(filePath, linkOptions))
    {
      System.out.println("file does not exist!");
    } else
    {
      System.out.println("file's status is unknown!");
    }
Psalmist answered 6/3, 2017 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.