'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!?
Solution 1:[1]
I was able to reproduce this under the following specific circumstances:
- The OS is Windows (implied by the path syntax)
- The path refers to a directory
- 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |