tags:

views:

1839

answers:

5

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?

+6  A: 

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.

Tom Hawtin - tackline
A: 

Does the same problem happen when you create the file using the absolute pathname?

File file = new File("C:\\Something\\Utilities\\data\\someTextFile.txt");

jhominal
yes, it doesn't seem to make a difference
atsjoo
A: 

If the situations where it fails involves running it as another user, and you're on Vista/Win7, it could be caused by VirtualStore, the mechanism where windows let an unprivileged user "write" places it normally cannot, the changes are however stored in "%USERPROFILE%\AppData\Local\VirtualStore\" which are private to each user-account.

Kjetil Jorgensen
I'm running on windows xp x86
atsjoo
A: 

try adding a / at the start so javac wont assume it's according to the Classpath. This happens a lot when you're working in an IDE, like Netbeans and you put this on the src directory... does it help?

ONi
+1  A: 

I am seeing a 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.

Roman Zenka
I just figured it out: http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4483097Apparently, 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!
Roman Zenka
@Roman: 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?
Odelya
@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.
Roman Zenka