file.exists() doesn't work

Hi, this seems really weird to me. I cannot make this simple code work. Here is what and how.

I have index.jsp which includes checkFile.jsp. This checkFile checks for existance of the file in the directory.

index.jsp is in the main directory. checkFile.jsp and the files to check are in the directory "data".

Code in index.jsp:

<%

String incFile = reuqest.getParameter("data");

incFile = incFile + ".htm"

%>

<%@ include file="data/checkFile.jsp"%>

Code in checkFile.jsp

<%

boolean f_exists = false;

File f = new File(incFile);

if(f.exists()){

f_exists = true;

}

%>

The result I get: f_exists is false. How come? Please, people, this is a quick and easy question, right?

Cheers,

Alex

P.S. It is Win2KPro, Tomcat 3.3.

[869 byte] By [a_pop] at [2007-9-19]
# 1
check the permissions of the file usually if they dont have the correct permissions it returns such and error.hope this helps
revasree at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
Nope - it didn't work for me. Thought the files were marked as archives (which is strange), it still returns false.Any other ideas?
a_pop at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

You probably are assuming you know what directory your path "data/checkFile.jsp" is relative to, and you are probably assuming incorrectly. Try the getAbsolutePath() method of your File object to see what you are actually checking.

By the way, the code you have there:boolean f_exists = false;

File f = new File(incFile);

if(f.exists()){

f_exists = true;

}

is not wrong, but could be written asFile f = new File(incFile);

boolean f_exists = f.exists();

DrClap at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hey, this problem is posted many a times in this forum. Your JSP will search for the file in 'bin' dir.

You can check this by placing your file in the Tomcat\bin directory. If it works then, give a relative path from your bin.

File file = new File("../webapps/application/data/"+incFile);

Sudha

sudha_mp at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thanks DrClap and sudha mp,I new there was something simple there. It's actually looking for the file in the Tomcat/Bin directory, right. So, yes, now I know what to do.Cheers,Alexp.s. DrClap was faster with the answer. Sorry, sudha mp. :)
a_pop at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6
> p.s. DrClap was faster with the answer. Sorry, sudha> mp. :)No problem at all. Your problem got solved, that matters to me. Better luck to me next time.Sudha
sudha_mp at 2007-7-4 > top of java,Enterprise & Remote Computing,Web Tier APIs...