aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

reading a text file from a JSP page

suneesh vasudevan
Greenhorn

Joined: Jun 26, 2003
Posts: 1
Hello
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
regards
Suneesh
Code which i used in jsp......
<%
com.StringReplace str1=new com.StringReplace();
String str="";
try{
java.io.File file =new java.io.File(register.txt);
StringBuffer message=new StringBuffer();
BufferedReader br=new BufferedReader(new FileReader(file));
while((str=br.readLine())!=null)
{
int i=str.indexOf("<UserName>");
if(i!=-1)
{
str=str1.replaceString(i, 10, str, "suneesh");
}
int j=str.indexOf("<Password>");
if(j!=-1)
{
str=str1.replaceString(j, 10, str, "suneesh");
}
message.append(str+"\n");

}
out.println(message.toString());
}catch(Exception e){System.out.println(e);}
%>
Malhar Barai
Author
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.
HTH
MB


Malhar Barai
SOA & Java Book
David O'Meara
Rancher

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.
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading a text file from a JSP page