why i cant read simple text file in jsp
Hello all
i have this simple function that will read me line by line from giving text file
that is in the under webapps dir of my tomcat
public String getConst(String category,String param){
String filename = "\\conf.txt";
try{
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader( filename) );
String str;
while ((str = in.readLine()) != null) {
str+=str;
}
return str;
}catch(java.io.IOException e){
return "file not found";
}
}
and stillim geting file not found all the time why is that?
Re: why i cant read simple text file in jsp
What is the actual exception error message?
In theory, there is no difference between theory and practice, but not in practice...
Anon.
Re: why i cant read simple text file in jsp
java.io.IOException
im geting
Re: why i cant read simple text file in jsp
Quote:
Originally Posted by umen
Hello all
i have this simple function that will read me line by line from giving text file
that is in the under webapps dir of my tomcat
public String getConst(String category,String param){
String filename = "\\conf.txt";
try{
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.FileReader( filename) );
String str;
while ((str = in.readLine()) != null) {
str+=str;
}
return str;
}catch(java.io.IOException e){
return "file not found";
}
}
and stillim geting file not found all the time why is that?
Your exception says it all , file is not found. Why ? wrong path,
check tomcat docs/mail list. Try the Tomcat forum
TC user forum
Re: why i cant read simple text file in jsp
Quote:
Your exception says it all , file is not found...
Actually the OP is throwing away the exception and returning his own "file not found" message. The only way you can really be sure of the cause of the exception is to not throw it away an use the infomation that the exception is providing (read: stack trace).
Re: why i cant read simple text file in jsp
Quote:
Originally Posted by gmrowe1075
Actually the OP is throwing away the exception and returning his own "file not found" message. The only way you can really be sure of the cause of the exception is to not throw it away an use the infomation that the exception is providing (read: stack trace).
Well , actually I saw what the OP is returning in that catch block, and also read the code for file read etc., Iam pretty sure it should be file not found with that path et al :)
Ok , I will be more specific try reading the javax.servlet, api on how to access files inside your web application running inside a container, read about the Interface javax.servlet.ServletContext.
Re: why i cant read simple text file in jsp
the file is for sure there ?
but mybe i can't read the file from the jsp page levle , mybe it need to be
servlet , i dont know..
Re: why i cant read simple text file in jsp
Try placing your file in the web-inf folder.
Re: why i cant read simple text file in jsp
Quote:
Originally Posted by umen
the file is for sure there ?
but mybe i can't read the file from the jsp page levle , mybe it need to be
servlet , i dont know..
as a start, you could paste us the contents of the exception stack trace (like was asked originally)? :)
Re: why i cant read simple text file in jsp
Here is what you should do.
// obtain a reference to ServletContext
ServletContext context = pageContext.getServletContext();
// create a file object to point to a directory residing on the server
// here, I am assuming that you have your file in the 'web' directory
File dir = new File( context.getRealPath( "web" ) );
// create a file object that encapsulates the file you wish to read
File file = new File( dir, "filename.extension" );
// now check that the file exists
if( file.exists() )
{
out.println( "<h1>File contains</h1>" );
BufferedReader buf = new BufferedReader( new FileReader( file ) );
String output = "";
while( ( output = buf.readLine() ) != null )
{
out.println( output );
} // ends while
// close buffer
buf.close();
} // ends if statement
else
{
out.println( "File does not exist." );
} // ends else
Please note that the servlet generated from running the JSP is ought to handle the IO exception for you, and that is why I haven't bothered including a try and catch block. However, if you wish to handle any IOExceptions within the JSP page, I don't see why you shouldn't do that.
I hope this helps.