It's not a secret anymore!
The moose likes JSP and the fly likes reading a text file from a JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "reading a text file from a JSP page" Watch "reading a text file from a JSP page" New topic

reading a text file from a JSP page

suneesh vasudevan

Joined: Jun 26, 2003
Posts: 1
Iam trying to read text file from a JSP page.I kept the text file in the same directory of jsp.But it is not able to locate the file. System
is giving file not found exceptio,
I tried the same code in an application .The its working fine
Can anybody help me
Code which i used in jsp......
com.StringReplace str1=new com.StringReplace();
String str="";
try{ file =new;
StringBuffer message=new StringBuffer();
BufferedReader br=new BufferedReader(new FileReader(file));
int i=str.indexOf("<UserName>");
str=str1.replaceString(i, 10, str, "suneesh");
int j=str.indexOf("<Password>");
str=str1.replaceString(j, 10, str, "suneesh");

}catch(Exception e){System.out.println(e);}
Malhar Barai
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
hi suneesh
Please give the absolute path of the file, as while compiling the jsp file, it won't be able to get the location of the file.
try giving the path.

Malhar Barai
SOA & Java Book
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

there is a (semi)related thread here
Basically, the 'default directory' is not what you think it is. You are assuming it is the directory where the JSP is, but it isn't.
The other thread shows how to place a file in the web-inf/classes directory (or somewhere else on the web app classpath - NOT the machine classpath, they are different ) and load it dynamically. It is better not to hard-code file paths since it will make your application non-portable.
I agree. Here's the link:
subject: reading a text file from a JSP page
It's not a secret anymore!