sikidhart
sikidhart

Reputation: 411

Files.exists(path) is false but file.exists() is true

Why does this code return false?

Path path = Paths.get("C:\\aaa\\bbb\\ccc");
Files.exists(path); // false!?

Even when I convert to it from a File (which exists):

File file = new File("C:\\aaa\\bbb\\ccc");
file.exists(); // true!!!
Path path = file.toPath();
Files.exists(path); // still false!?

Upvotes: 3

Views: 999

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

I was able to reproduce this under the following specific circumstances:

  1. The OS is Windows (implied by the path syntax)
  2. The path refers to a directory
  3. The directory is read-only or, the user does not have "List Folder Contents" permission.

I tested this on Linux (Centos 6) and cannot reproduce it even when changing the filemode on the directory (i.e. chmod -x /aaa/bbb/ccc or chmod -r /aaa/bbb/ccc)

So this appears to occur only on Windows. There must be some difference between how java.io and java.nio.file implement existence testing with regards to file permissions on Windows.

Check the permissions on the directory.

This may be a bug worth reporting.

Upvotes: 5

Related Questions