Sunday, February 5, 2017

Reading a Text File , Using JSP

Step 1.....................................................

To read a  text file ,You can use  following  way .  First  create a  .txt  you want  to  read , Then  copy  the path to  your  text  file . 

Ex: "C:\Users\Roshan\workspace\ReadingFromTextFile\WebContent\quotes.txt"

Then reaname  it  as , this way 

"C:\\Users\\Roshan\\workspace\\ReadingFromTextFile\WebContent\\quotes.txt" 

(Used \\  ,instead  of  \  )

In   JSP file you  create , Replace your  text file location instead what  I have  used . 

Below  you  can see  text  file I have  used


 The mind is everything. What you think you become.........  Buddha
 Whatever the mind of man can conceive and believe, it can achieve.....Napoleon Hill
 I attribute my success to this: I never gave or took any excuse..........Florence Nightingale
 I've missed more than 9000 shots in my career. I've lost almost 300 games. 26 times I've been trusted to take the game winning shot and missed. I've failed over and over and over again in my life. And that is why I succeed. ...............Michael Jordan
 Twenty years from now you will be more disappointed by the things that you didn’t do than by the ones you did do, so throw off the bowlines, sail away from safe harbor, catch the trade winds in your sails.  Explore, Dream, Discover. ........Mark Twain
 Life is 10% what happens to me and 90% of how I react to it..........Charles Swindoll
 The best time to plant a tree was 20 years ago. The second best time is now.......Chinese Proverb
 Your time is limited, so don’t waste it living someone else’s life...........Steve Jobs

 

Step 2 ..................................................

Then lets  create JSP  file .You can use  following code for it .

<%@  page import ="java.io.*"%>
<%@  page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
<!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 >Reading  a Text  File.............</title>
</head>
<body style="background-color:powderblue;">
    <h2> Famous  Quotes </h2>
    <%
        try
        {
           
            out.println("<ol>");
            BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Roshan\\workspace\\ReadingFromTextFile\\WebContent\\quotes.txt"));
           
            String  str ;
            while((str= in.readLine())!= null)
            {
                out.println("<li>"+ str + "</li>") ;
            }
            out.println("</ol>");
            in.close();
        }
         catch(IOException ioe)
        {
             out.println("An exception occured " + ioe)  ;
        }
    %>
</body>
</html>

 

Step 3................................................

Run  your JSP  file .You  will  able  get  a  output  like  this .

 

No comments:

Post a Comment

The AI Driven Software Developer, Optimize Innovate Transform

  The AI-Driven Software Developer: Optimize, Innovate, Transform": AI Transformation in Software Development : Understand how AI is re...