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]

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();
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
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. :)