Gong Anna wrote:
> Hi,
> Could some one tell me how to read from a text file in the same folder as my
> .jsp pages? I tried using FileReader("file.txt"), but it's not finding the
> .txt file (I think it's looking in the class path instead of the current
> folder for the file). Some one please help! Thanks.
> Anna
>
Using a file reader (as you described) is unlikely to work, because it looks in
the servlet container's current working directory.
To do this from within a JSP page, you will be best off using the getResource() or
getResourceAsStream() methods of ServletContext, which already know how to access
things from your applications document root.
Assuming your JSP pages are at the top level directory (so you use a URL like
"http://www.mycompany.com/mypage.jsp" to access them), you would be able to read
your file like this:
<%
InputStream is =
getServletContext().getResourceAsStream("/file.txt");
if (is == null) {
... report the problem ...
}
FileReader reader = new FileReader(is);
...
%>
Craig McClanahan
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html