By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

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

P: 38
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
Dec 14 '07 #1
Share this Question
Share on Google+
10 Replies


BigDaddyLH
Expert 100+
P: 1,216
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. }
Dec 14 '07 #2

Expert 10K+
P: 11,448
Could it be a slash versus backslash issue?

kind regards,

Jos
Dec 14 '07 #3

jeffbroodwar
100+
P: 118
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
Dec 14 '07 #4

P: 38
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
Dec 26 '07 #5

dmjpro
100+
P: 2,476
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
Dec 26 '07 #6

P: 38
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.
Dec 26 '07 #7

dmjpro
100+
P: 2,476
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
Dec 26 '07 #8

P: 38
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?
Dec 26 '07 #9

P: 38
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 !
Dec 26 '07 #10

dmjpro
100+
P: 2,476
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
Dec 26 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.