Connecting Tech Pros Worldwide Forums | Help | Site Map

File fp = new File(filePath);fp.exists() does not yeild proper result on Linux

Member
 
Join Date: May 2007
Posts: 40
#1: Dec 14 '07
Below given is the code to check if a file exists.
This code works on Windows but on Linux it fails even when the file exists.

Please let me know if you have a solution or a work around for this.
Has anybody experienced the same problem.

boolean DoesTheFileExist(String filePath)
{
File fp = new File(filePath);
if(true == fp.exists())
{
return true;
}
else
{
return false;
}
}

Regards,
Ganesh

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Dec 14 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


It should work. Of course UNIX != Windows. You should take a long look at the path you are passing to this method. By the way, you could have written it more simply as
Expand|Select|Wrap|Line Numbers
  1. boolean DoesTheFileExist(String filePath) {
  2.     return new File(filePath).exists();
  3. }
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Dec 14 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


Could it be a slash versus backslash issue?

kind regards,

Jos
jeffbroodwar's Avatar
Member
 
Join Date: Oct 2006
Posts: 118
#4: Dec 14 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


I think Jos is right. there are some problems that occur when your using windows and linux in terms of syntax. guess you better check the documentation for linux.


P.S. StarCraft II coming soon... En taro adun !!!


Regards,
Jeff
Member
 
Join Date: May 2007
Posts: 40
#5: Dec 26 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


Quote:

Originally Posted by JosAH

Could it be a slash versus backslash issue?

kind regards,

Jos

it is not a slash versus backslash issue.

Has anyone used this kind of code on linux,
i am really facing problems with this exists() API.
it is really causing problems.

I gave the path as /tmp/file.txt on linux and c:\file.txt on windows.
On windows i get the correct answer but on linux the
API returns false even when the file exists.

Also for your information i am using "jdk1.5.0_13", i don't think that is an issue or is it?

Java gurus , please come forward and answer this problem.

Please ,anybody who has used this API on Linux please come forward and post your thoughts.

Please can any body shed some light on this issue, i am eagerly waiting for more posts

regards,
Ganesh
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: Dec 26 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


Quote:

Originally Posted by ganeshp

it is not a slash versus backslash issue.

Has anyone used this kind of code on linux,
i am really facing problems with this exists() API.
it is really causing problems.

I gave the path as /tmp/file.txt on linux and c:\file.txt on windows.
On windows i get the correct answer but on linux the
API returns false even when the file exists.

Also for your information i am using "jdk1.5.0_13", i don't think that is an issue or is it?

Java gurus , please come forward and answer this problem.

Please ,anybody who has used this API on Linux please come forward and post your thoughts.

Please can any body shed some light on this issue, i am eagerly waiting for more posts

regards,
Ganesh


I think it is a Linux issue.
On which user your are running this Script.
May be from that user the file can't be accessed.
Though I am not expert on Unix.


Debasis Jana
Member
 
Join Date: May 2007
Posts: 40
#7: Dec 26 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


Quote:

Originally Posted by dmjpro

I think it is a Linux issue.
On which user your are running this Script.
May be from that user the file can't be accessed.
Though I am not expert on Unix.


Debasis Jana

Thanks Debasis Jana for your interest.

I am running it as a root user.
Since the use is root, i guess there should be no previlage related issues.
Also the permission for the file is rw-r-r. So any user should be able to read this file.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#8: Dec 26 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


Quote:

Originally Posted by ganeshp

Thanks Debasis Jana for your interest.

I am running it as a root user.
Since the use is root, i guess there should be no previlage related issues.
Also the permission for the file is rw-r-r. So any user should be able to read this file.

The case sensitivity check please.
May be it is sure shot answer. :)

Debasis Jana
Member
 
Join Date: May 2007
Posts: 40
#9: Dec 26 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


Quote:

Originally Posted by dmjpro

The case sensitivity check please.
May be it is sure shot answer. :)

Debasis Jana


I tried to print the absolute path.

Even thought i enter the path as "/tmp/file.txt"
when i print the absolute path, the output is "c:\tmp\file.txt"

i.e. fp.getAbsolutePath() prints "c:\tmp\file.txt" when i enter "/tmp/file.txt".

I do not know how this conversion is taking place , because i am not making this conversion.

does anybody know whats happening here?
Member
 
Join Date: May 2007
Posts: 40
#10: Dec 26 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


My code is a web application, this was written on windows using eclipse,
Ant was used to build the code on windows.
Tomcat is the web-server that i am using and it is running on windows.

Say for example link for my web page is abc.com
Here i ask user to specify absolute path of a file on his system.
Now when the user is accessing my web-page form Linux box he enters the file path as
/tmp/file.txt which is correct as he is using the Linux machine.

I debugged my application just now ,
in code the File fp = new File(filePath);
the path is still /tmp/file.txt , But when is do fp.getAbsolutePath() the string returned is C:\tmp\file.txt
This is incorrect right ? because the user is on Linux-box and he entered /tmp/fiile.txt
And fp.exists() returns false.

I am not understanding this behavior !
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#11: Dec 26 '07

re: File fp = new File(filePath);fp.exists() does not yeild proper result on Linux


Quote:

Originally Posted by ganeshp

I tried to print the absolute path.

Even thought i enter the path as "/tmp/file.txt"
when i print the absolute path, the output is "c:\tmp\file.txt"

i.e. fp.getAbsolutePath() prints "c:\tmp\file.txt" when i enter "/tmp/file.txt".

I do not know how this conversion is taking place , because i am not making this conversion.

does anybody know whats happening here?


I am little bit confusing here.
Let me understand.
Please send me the code fragment what you tried in Unix and in Windows.
And print the output as you are getting.
And paste that "getAbsolutePath()" also.

Debasis Jana
Reply