File.exists() returns false when file exists

104,955

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.

Solution 5

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.

Share:
104,955
atsjoo
Author by

atsjoo

Anders

Updated on December 27, 2021

Comments

  • atsjoo
    atsjoo over 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
    atsjoo almost 15 years
    I'm running on windows xp x86
  • Roman Zenka
    Roman Zenka almost 14 years
    I 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
    Dejell over 13 years
    I 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
    Roman Zenka over 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
    Clément over 12 years
    Interesting. Can you expand on this? Which specific permissions do you have in mind?
  • Theblacknight
    Theblacknight about 12 years
    I 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
    aafc about 10 years
    God Windows sucks so much.
  • Stephen C
    Stephen C over 9 years
    I wouldn't expect file:/C:/DEV/test.txt to 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
    atsjoo almost 9 years
    I 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
    Homr Zodyssey about 8 years
    3.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
    P-S about 8 years
    For 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
    ceph3us almost 8 years
    small 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 :)
  • рüффп
    рüффп over 7 years
    I think the issue is more related to absolute path vs relative path. The slash are valid in Java even for Windows paths.
  • Alfira
    Alfira about 7 years
    Here 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
    RAM237 almost 7 years
    OMG, 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 exists ;)
  • Bato-Bair Tsyrenov
    Bato-Bair Tsyrenov over 5 years
    Consider creating a separate QA for this answer and comments
  • Ganesan J
    Ganesan J about 3 years
    sorry I don't know how to about format code in stackoverflow
  • Ganesan J
    Ganesan J about 3 years
    Thanks for formatting the code.Thank you Sir
  • Daniel
    Daniel almost 3 years
    I 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
    Daniel almost 3 years
    getAbsoluteFile() works but now I am afraid this fails in similar situations where I expect relative paths to work....