5

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>
Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47
farissyariati
  • 365
  • 4
  • 9
  • 21

5 Answers5

6

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";
Nissa
  • 4,636
  • 8
  • 29
  • 37
Ahmed Hashem
  • 360
  • 1
  • 10
2

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.

Rakesh Patel
  • 393
  • 2
  • 10
0

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.

mike
  • 1
0

Try to locate it from application's root path.

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

Siva Arunachalam
  • 7,582
  • 15
  • 79
  • 132
0

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.

McDowell
  • 107,573
  • 31
  • 204
  • 267
  • About your last statement, how should them be done then? Any reference please? Thanks in advance – Btc Sources Mar 01 '15 at 20:01
  • @BtcSources I assume you are talking about my comment on scriptlets. [BalusC](http://stackoverflow.com/users/157882) has a page on this: [How to avoid Java code in JSP files?](http://balusc.blogspot.co.uk/2010/07/how-to-avoid-java-code-in-jsp-files.html) – McDowell Mar 04 '15 at 09:39