|
-
August 10th, 2006, 04:36 PM
#1
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?
-
August 10th, 2006, 05:41 PM
#2
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.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
August 11th, 2006, 01:24 AM
#3
Re: why i cant read simple text file in jsp
java.io.IOException
im geting
-
August 11th, 2006, 01:41 AM
#4
Re: why i cant read simple text file in jsp
 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
"It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts." Quote from Sherlock Holmes
-
August 11th, 2006, 02:11 AM
#5
Re: why i cant read simple text file in jsp
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).
-
August 11th, 2006, 02:34 AM
#6
Re: why i cant read simple text file in jsp
 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.
"It is a capital mistake to theorize before one has data. Insensibly one begins to twist facts to suit theories, instead of theories to suit facts." Quote from Sherlock Holmes
-
August 11th, 2006, 04:05 PM
#7
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..
-
August 12th, 2006, 11:14 PM
#8
Re: why i cant read simple text file in jsp
Try placing your file in the web-inf folder.
"The Chicken and Rice MRE is not a personal lubricant."
-
August 13th, 2006, 04:28 AM
#9
Re: why i cant read simple text file in jsp
 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)?
-
August 24th, 2006, 03:34 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|