In this Example we will discuss about how to read text file with the JSP objects. First we create a text file that contains strings in it. In the program we use input and output stream for execute the expression.
We put the text file in the WEB/INF folder where it will be fetched in the program we just give the reference of this folder. For storing the content of a file we also use the buffered reader function. readLine method also used to read the content of text file stored in WEB/INF folder.
<%@ page import=”java.io.* “%>
<html>
<head>
<title><ReadingTextFile> In JSP</title>
</head>
<body bgcolor=”#E1EDF3″>
<h3 align=”center”><br><%
String fileName = “/WEB-INF/Reading.txt”;
InputStream instrm = application.getResourceAsStream(fileName);
try
{
if(instrm == null)
{
response.setStatus(response.SC_NOT_FOUND);
}
Else
{
BufferedReader bfrdr = new BufferedReader((new InputStreamReader(instrm)));
String cntnt;
while((cntnt= bfrdr.readLine())!= null)
{
out.println(cntnt+”<br>”);
}
}
}
catch(IOException e1)
{
out.println(e1.getMessage());
}
%></h3></br>
</body>
</html>