by Dinesh Thakur Category: JSP Elements

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>&lt;ReadingTextFile&gt; 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>

         Jsp readingTextFile Example





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.