JSP Reading Text File

JSP Reading Text File


Posted in : Java Posted on : May 16, 2012 at 7:12 PM Comments : [ 0 ]

In this tutorial you will learn about how to read text file in JSP.

JSP Reading Text File

In this tutorial you will learn about how to read text file in JSP.

In this example we will discussed about how to read text file from the JSP page. To read the text file from JSP page I have created a text file in the WEB-INF folder and then created a JSP page into the WebContent folder onto which stored the file path into a InputStream class using getResourceAsStream() method of implicit object application. To store the content of a file used the BufferedReader class and to read the file contents used its method readLine() that reads the contents of file line by line.

readingText.txt

Aman
Brijesh
Manish
Noor
Rajesh
Shashi
Vinay
Zayed

jspReadingTextFile.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.io.*, java.net.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Reading Text File</title>
</head>
<body>
<%
String fileName = "/WEB-INF/readingText.txt";
InputStream ins = application.getResourceAsStream(fileName);
try
{
if(ins == null)
{
response.setStatus(response.SC_NOT_FOUND);
}
else
{
BufferedReader br = new BufferedReader((new InputStreamReader(ins)));
String data;
while((data= br.readLine())!= null)
{
out.println(data+"<br>");
}
} 
}
catch(IOException e)
{
out.println(e.getMessage());
}
%>
</body>
</html>

Output

When you will execute the above JSP page you will get the output as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics