File.exists() returns false when file exists
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.
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 f = new File(filename); f.exists();
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
File f = new File(filename); f.exists();
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.
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.
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.
When ["Hide extensions for known file types."] is checked windows open "t.txt.txt" when type "t.txt" in [explorer]/[run windows] but programmatically not.
atsjoo almost 2 years
I've encountered a bug I can't seem to find any logic behind. I have this File object, which is created like this:
File file = new File("utilities/data/someTextFile.txt");
I then do
file.exists(), and it returns
false(!?). If the file is not found, I'm logging
f.getAbsolutePath()to a file. When I look at the path, it seems OK. I can copy-paste the complete path into the "Run"-window in Windows and the file opens fine.
The file exists at all times and is not deleted nor changed during the running of my application. It is located at the local machine.
This only seems to occur in certain situations. I can reproduce the fault at any time, but I'm sure the path of the file object is not changed by the actions I make to reproduce the fault.
What can cause
file.exists()to return false? Does this have something to do with permissions or file locks, etc.?
atsjoo over 14 yearsI'm running on windows xp x86
Roman Zenka over 13 yearsI just figured it out: bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4483097 Apparently, the operations running on file are resolved against the current directory, while getAbsolutePath resolves against user.dir. If these two paths do not match, you get conflicting results. Devilish!
Dejell about 13 yearsI have the exact same problem I tried to use both methods to check if file exists, and still I get false on Windows 7 only! Any idea?
Roman Zenka about 13 years@Odelya: What IDE are you using? What is your -Duser.dir set to? My problem was caused by setting -Duser.dir to a different directory than the current working one.
Clément about 12 yearsInteresting. Can you expand on this? Which specific permissions do you have in mind?
Theblacknight almost 12 yearsI had this issue, and the problem was that I created a txt file, which was called 'testFile.txt', in C:\test. I referred to this file using the path C:\test\testFile.txt, which didn't work. It was because the file had actually been saved as testFile.txt.txt, hence the up vote on the above solution (Old question, but no accepted answer!)
aafc almost 10 yearsGod Windows sucks so much.
Stephen C about 9 yearsI wouldn't expect
file:/C:/DEV/test.txtto work as a pathname. It's a URL not a pathname. While some people make this mistake, there is no evidence that the OP has ...
atsjoo over 8 yearsI don't think this was the issue in my case. As mentioned in my question: "I can copy-paste the complete path into the "Run"-window in Windows and the file opens fine.", which means the file actually does exist.
Homr Zodyssey over 7 years3.5 years later, and I ran into the same issue. I had an autorun script set up to configure environment variables each time I launched cmd.com. It didn't even change the current directory -- just some doskey macros and some environment variables. I removed the autorun, and just manually ran the commands in the file, and suddenly File.exists() works correctly.
P-S over 7 yearsFor anyone who is working on a Dynamic Web Project, using file.exists() will throw an exception, use file.getAbsoluteFile().exists() to check for files in the WEB-INF directory (general tip, not Windows 7 specific).
ceph3us over 7 yearssmall explanation: every call to constructor by use of new keyword creates an Object - same as in this case an Object described by Class which name is File ! so not a instance of File != descriptors :)
рüффп about 7 yearsI think the issue is more related to absolute path vs relative path. The slash are valid in Java even for Windows paths.
Alfira over 6 yearsHere can be java.nio.file.AccessDeniedException blocking ability to reach out file/dir existence. For instance, if you keep dir opened in FAR or other file explorer, then delete dir with all nested files and check existence of this dir, then you can get AccessDeniedException (extends IOException) for temporary file kept for you. In this case Files.exists returns false for IOException.
RAM237 over 6 yearsOMG, it really works (both of them), I was just dumbly checking for the wrong file and came across this question to find out why none of them work for me :) BTW, it seems the
()are missing in the second line after
Bato-Bair Tsyrenov almost 5 yearsConsider creating a separate QA for this answer and comments
Ganesan J over 2 yearssorry I don't know how to about format code in stackoverflow
Ganesan J over 2 yearsThanks for formatting the code.Thank you Sir
Daniel over 2 yearsI have the same problem but I am absolutely positively sure that this works within the IDE, but somehow not when run from our Standalone application.
Daniel over 2 yearsgetAbsoluteFile() works but now I am afraid this fails in similar situations where I expect relative paths to work....