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>
                To read the text file from JSP page I have created a text file in the WEB-INF folder and then created a JSP page into the WebContent folder onto which stored the file path into a InputStream class using getResourceAsStream() method of implicit object application.
How to open a JSP file. You can open and edit a JSP file in any text or source code editor, such as Microsoft Visual Studio Code (cross-platform) or GitHub Atom (cross-platform). You can also view the file in any web browser.
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";
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With