File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java.io.File.exists()  returns false, when the file is actually existing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.io.File.exists()  returns false, when the file is actually existing" Watch "java.io.File.exists()  returns false, when the file is actually existing" New topic
Author

java.io.File.exists() returns false, when the file is actually existing

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 530
    
    2

Hello All,

I'm stuck with a basic issue related to File I/O operations. I have the below code as

My project structure is as follows

C:\Workspace\LearnJava\src\org\learning\java\MyFiler.java
C:\Workspace\LearnJava\src\resources\sample.properties



and the output is



As you see, the first two methods return me the absolutePath and canonicalPath, but the absolutePath is actually "C:\Workspace\LearnJava\src\resources\sample.properties" , "src" is missing in the output.

But the real question is, when getAbsolutePath() returns me an output, whether correct or not, why the other methods returning me false. If exists() is returning false, that means it could not find the file, but then it should not return the absolutePath() also.

If I change the resource folder to the LearnJava folder level instead of src level, then the output is

C:\Workspace\LearnJava\resources\sample.properties
C:\Workspace\LearnJava\resources\sample.properties
false
true
true
true
true

What is the mistake I'm doing here ?

Regards
KumarRaja

Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

The problem is that your program's working directory is:

C:\Workspace\LearnJava

whereas you thought that it is C:\Workspace\LearnJava\src.


PM Me If You Want to get Urgent Help on Java Programming
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 530
    
    2

I agree with you on the path of the file.

But why would f.getAbsolutePath(); return me some value (may not be correct) at first place, while f.exists() is failing
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Because you specify the wrong path then the file does not exist. The getAbsolutePath() method returns the path you specified, it doesn't necessarily return an existing path.
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 530
    
    2

Thank you Nam Ha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.File.exists() returns false, when the file is actually existing