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?

Morteza Adi
  • 2,413
  • 2
  • 22
  • 37
  • 1
    Which package are you working in? java.io.* does not have a File class with a notExists() method. – Kon Jan 24 '14 at 14:13
  • 1
    java.nio.file.Files -- JDK7 – Morteza Adi Jan 24 '14 at 14:14
  • It's Files, not File. – peter.petrov Jan 24 '14 at 14:14
  • 6
    The link you provide says it right there: _If both exists and notExists return false, the existence of the file cannot be verified._. – Keppil Jan 24 '14 at 14:15
  • 3
    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.**" – forgivenson Jan 24 '14 at 14:16
  • @Keppil 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!! – Morteza Adi Jan 24 '14 at 14:19
  • @forgivenson 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() – Morteza Adi Jan 24 '14 at 14:22
  • @MortezaAdi: They are definitely not redundant. A file doesn't have to exist just because `notExists()` returns false, and vice versa. – Keppil Jan 24 '14 at 14:31
  • @Keppil 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. – Morteza Adi Jan 24 '14 at 14:46
  • I agree that the javadocs are very unclear, even though they may be perfectly clear to some. – Lambart Sep 14 '16 at 23:01

4 Answers4

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

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • 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 ?? – Morteza Adi Jan 24 '14 at 14:24
  • 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. – peter.petrov Jan 24 '14 at 14:31
  • 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. – Morteza Adi Jan 24 '14 at 14:45
  • OK everyone can have an opinion. I am not defending any decisions design here. Just trying to answer your question. – peter.petrov Jan 24 '14 at 14:54
  • 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 :) – Morteza Adi Jan 24 '14 at 15:03
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.

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
  • 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 ?? – Morteza Adi Jan 24 '14 at 14:25
  • 1
    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. – Leonard Brünings Jan 24 '14 at 14:33
  • 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. – Morteza Adi Jan 24 '14 at 14:43
  • 1
    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. – Leonard Brünings Jan 24 '14 at 14:47
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.

laune
  • 31,114
  • 3
  • 29
  • 42
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!");
    }
ravthiru
  • 8,878
  • 2
  • 43
  • 52