title: How to Read a TXT file in Java Server Page Directory
body: <p>I'd like to create an app that requires to read a <code>.txt</code> file on my project directory. </p>
  
  <p>This is my code of my <code>index.jsp</code>: </p>
  
  <pre><code>&lt;%@page import="java.io.FileReader"%&gt;
  &lt;%@page import="java.io.BufferedReader"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;Read Text&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;%
              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());
          %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I execute the above code, my browser tells me that <code>aFile.txt</code> cannot be found. Then, I've put <code>aFile.txt</code> in the same directory as this web page runs (<code>index.jsp</code>).
  I wonder, what should I write to locate the directory of <code>aFile.txt</code></p>
  
  <p>And this is how my problem was solved. Thanks Ahmad hasem</p>
  
  <pre><code>&lt;%@page import="java.io.File"%&gt;
  &lt;%@page import="java.io.InputStreamReader"%&gt;
  &lt;%@page import="java.net.URL"%&gt;
  &lt;%@page import="java.io.FileReader"%&gt;
  &lt;%@page import="java.io.BufferedReader"%&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;Read Text&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;%
              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());
          %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>