Reading Text Files Through Servlet
843841Aug 14 2005 — edited Sep 12 2007Hi All,
What i am trying to do is read a text file straight through a .jsp file? Firstly is this possible? Ive been stressing over this for days now, I'm fairly new to jsp with background in PHP and although ive grasped the basics, some the syntax nevertheless still seems to confuse. Right, if you people would be so kinds as to have a look at this :
<%@page import="java.io.*" import="java.net.URL"%>
<%!public static final String Results = "text.txt";%>
<%!
public void showResults()
{
File resultsFile = makeAbsoluteFilename(Results);
BufferReader reader = null;
try
{
reader = new BufferedReader(new FileReader(resultsFile));
System.out.println("<b>Results...</b><br>");
String line;
line = reader.readLine();
while(line != null)
{
System.out.println("CONTENTS OF TEXT FILE: " + line);
line = reader.readLine();
}
System.out.println();
}
catch(FileNotFoundException e)
{
System.out.println("<b>Error Encountered reading...</b><br>" + resultsFile);
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch(IOException e)
{
System.out.println("<b>Error on closing: </b><br>" + resultsFile);
}
}
}
}
%>
File Structure
<install directory>\webapp\ROOT\APPS2
This is also where the text file is located (called text.txt). This is what i am trying to read and display the out put on the screen.
Now, when I run this on my Tomcat 5.5 with JDK 1.5 (all configed properly to run on LOCALHOST) it throws back 2 errors...
1). An error occurred at line: 5 in the jsp file: /apps2/read2.jsp
Generated servlet error:
The method makeAbsoluteFilename(String) is undefined for the type read2_jsp
2). An error occurred at line: 5 in the jsp file: /apps2/read2.jsp
Generated servlet error:
BufferReader cannot be resolved or is not a type
Guys ive spent all weekend working on this and i just cant find enough resources or figure our where im going wrong! Any one who can help, I would be most greatful!
Kind Regards,
Imran Sheiakh