farissyariati
farissyariati

Reputation: 365

How to Read a TXT file in Java Server Page Directory

I'd like to create an app that requires to read a .txt file on my project directory.

This is my code of my index.jsp:

<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Read Text</title>
    </head>
    <body>
        <%
            BufferedReader reader = new BufferedReader(new FileReader("aFile.txt"));
            StringBuilder sb = new StringBuilder();
            String line;

            while((line = reader.readLine())!= null){
                sb.append(line+"\n");
            }
            out.println(sb.toString());
        %>
    </body>
</html>

When I execute the above code, my browser tells me that aFile.txt cannot be found. Then, I've put aFile.txt in the same directory as this web page runs (index.jsp). I wonder, what should I write to locate the directory of aFile.txt

And this is how my problem was solved. Thanks Ahmad hasem

<%@page import="java.io.File"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Read Text</title>
    </head>
    <body>
        <%
            String jspPath = session.getServletContext().getRealPath("/res");
            String txtFilePath = jspPath+ "/aFile.txt";
            BufferedReader reader = new BufferedReader(new FileReader(txtFilePath));
            StringBuilder sb = new StringBuilder();
            String line;

            while((line = reader.readLine())!= null){
                sb.append(line+"\n");
            }
            out.println(sb.toString());
        %>
    </body>
</html>

Upvotes: 5

Views: 51785

Answers (5)

Ahmed Hashem
Ahmed Hashem

Reputation: 360

To get the directory of the running JSP, you can call the following code :

String jspPath = session.getServletConfig().getServletContext().getRealPath("/");

This code assumes that the JSP resides on the root of your web application. Then, you can append the txt file name to the jspPath

String txtFilePath = jspPath + java.util.File.separator + "aFile.txt";

Upvotes: 6

mike
mike

Reputation: 1

As I have experienced it will read from the root directory of your webserver. Actually from the directory you started the webserver. In Tomcat it will be /home/mike/tomcat. Naturally, because it was from this directory you started your java virtual machine and java will consider this its root directory.

Upvotes: 0

McDowell
McDowell

Reputation: 108899

You should not assume that the file can be read using the file system. It is a deployment and implementation detail as to whether the WAR file is "exploded" or not.

Assuming that aFile.txt is in the root of your application, you should be able to open the stream using the servlet context:

<% java.io.InputStream in = application.getResourceAsStream("/aFile.txt"); %>

There are also more appropriate ways to inline other files into a JSP.

The JSP include standard action:

<jsp:include page="aFile.txt" />

The JSTL import tag:

<%-- header --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
....
<c:import url="/aFile.txt" />

I am assuming this code is being written for educational purposes. No modern application should include <% %> scriptlets.

Upvotes: 0

Rakesh Patel
Rakesh Patel

Reputation: 383

where is jsp file and text file.are you put both in web-inf folder.please remove txt file from web-inf because you are need servlet if you want to access txt file from web-inf.

use the below code for get the path of the text file.

       this
      .getServlet()
      .getServletContext()
      .getRealPath(FOLDER NAME)
      .concat(System.getProperty("file.separator")
      .concat(FILE NAME));

pass the above code in file object.

Upvotes: 2

Siva Arunachalam
Siva Arunachalam

Reputation: 7740

Try to locate it from application's root path.

http://www.java2s.com/Code/Java/JSP/ReadingBinaryData.htm

Upvotes: 0

Related Questions