Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.exists() returns false when file exists

Tags:

java

file-io

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.?

like image 686
atsjoo Avatar asked May 28 '09 09:05

atsjoo


People also ask

Which method of the class returns true if the file exist otherwise returns false?

exists(): Returns true if the file exists; false if the file does not exist or its existence cannot be determined.

How does a exists () method work in Java?

File exists() method in Java with examples The exists() function is a part of the File class in Java. This function determines whether the is a file or directory denoted by the abstract filename exists or not. The function returns true if the abstract file path exists or else returns false.

How can I tell if a text file exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .


1 Answers

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.

like image 139
Roman Zenka Avatar answered Oct 14 '22 22:10

Roman Zenka