File.exists() returns false when file exists

Solution 1:

I am seeing the following situation on Windows 7:

file.exists() == false
file.getAbsoluteFile().exists() == true

The file in question is "var\log", the absolute path does refer to an existing file that is in a normal subdirectory (not a virtual store). This is seen from the IDE.

Solution 2:

It seems like there is a difference on how the path is specified in Java.

For example, if the file path is specified as file:/C:/DEV/test.txt then

File f = new File(filename);
f.exists();

will return false. The path might work in the explorer or in the browser, but it is a URL and not absolute file path.

But on the other hand if the file path is specified as C:/DEV/test.txt then

File f = new File(filename);
f.exists();

will return true because the path is not a URL, but it is a absolute path.

With Spring Framework that is exactly what ResourceUtils.getFile(filename) does - where name can be either a URL or the absolute file path.

Solution 3:

If the process does not have permissions to tell whether a file exists it will return false. It may be possible to open a file, but not tell by normal methods if it exists.

Solution 4:

The above answers didn't help out in my case. As stated above, I had:

file.exists() => false
file.getAbsoluteFile().exists => true

The root cause for this was that the Windows 7 machine owner had modified the registry for CMD so that it would autorun a command to launch in a specific directory to work with Python. This modification crippled the Java 1.6 code which apparently uses CMD on Windows for certain file operations, such as exists(). Eliminating the autorun from the registry solved the issue.