reading files(*.txt) from JSP

Hi, does anyone know how to read a text file saved on my directory, to a JSP page?
[96 byte] By [des_xu] at [2007-9-26 1:16:05]
# 1

hi des xu,

Thefollowing code might be easier to follow, it reads a file named 'newsfile.txt' in the same directory as this JSP file, and puts the content into a string named 'news'.

<HR>

<PRE>

<%@ page import = "java.io.*, java.util.*" %>

<%

// Read in the text file newsfile.txt

ServletContext context = getServletContext();

String file = (context.getRealPath("/newsfile.txt"));

String s, news = new String();

try {

DataInputStream in =

new DataInputStream(

new BufferedInputStream(

new FileInputStream(file)));

while((s = in.readLine())!= null) {

news += s + "\n";

}

in.close();

} catch(Exception e) {

System.out.println("NewsFile read: " +e);

}

%>

(Surround next line with HTML PRE, or use HTML tags in text file!)

<%=news%>

</PRE>

Regards,

Tirumalarao

Developer TechnicalSupport,

Sun MicroSystem,India.

rao_indts at 2007-6-29 0:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
oh..., thanks alot for your help!!!
des_xu at 2007-6-29 0:43:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...