Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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!?
like image 892
sikidhart Avatar asked Nov 23 '25 02:11

sikidhart


1 Answers

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.

like image 98
Jim Garrison Avatar answered Nov 25 '25 17:11

Jim Garrison