Monday, July 2, 2012

Small JSP to display the content of a file

pass the filename as ?file=/path/to/myfile.txt

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

<%

String filename = request.getParameter("file");
FileReader fr = new FileReader(filename); 
BufferedReader br = new BufferedReader(fr);
String line;
while ( (line = br.readLine() ) != null ) {
    out.write(line);
    out.write("\n");
}
br.close();
fr.close();
%>

No comments: