Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You shouldn't use the <code>File</code> API to reference relative webcontent files. The <code>File</code> API operates relative to the current working directory on the local disk file system, which is dependent on how you started the webserver and is in no way controllable from inside the webapplication's code.</p> <p>In this particular case you should rather use <a href="http://download.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResource%28java.lang.String%29" rel="nofollow noreferrer"><code>ServletContext#getResource()</code></a> or <a href="http://download.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String%29" rel="nofollow noreferrer"><code>getResourceAsStream()</code></a>:</p> <pre><code>URL resource = getServletContext().getResource("/WEB-INF/music/rock/Evanascence/Fallen/text.txt"); // ... </code></pre> <p>or</p> <pre><code>InputStream input = getServletContext().getResourceAsStream("/WEB-INF/music/rock/Evanascence/Fallen/text.txt"); // ... </code></pre> <p><em>(note that those double forward slashes are totally unnecessary, I already removed them)</em></p> <p>If it returns <code>null</code> then it don't exist. To wrap it in a <code>Reader</code>, use <a href="http://download.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html" rel="nofollow noreferrer"><code>InputStreamReader</code></a> wherein you specify the proper character encoding which should be the same as the file is originally saved in:</p> <pre><code>Reader reader = new InputStreamReader(input, "UTF-8"); // ... </code></pre> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream">getResourceAsStream() vs FileInputStream</a></li> </ul> <hr> <p><strong>Unrelated</strong> to the concrete question, needless to say that the code above <a href="https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files">belongs</a> in a <a href="https://stackoverflow.com/tags/servlets/info">servlet</a>, not in a JSP as you tagged. Inside a JSP, you may find the <a href="https://stackoverflow.com/tags/jstl/info">JSTL</a>'s <a href="http://download.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/import.html" rel="nofollow noreferrer"><code>&lt;c:import&gt;</code></a> tag more useful.</p> <pre class="lang-html prettyprint-override"><code>&lt;c:import url="/WEB-INF/music/rock/Evanascence/Fallen/text.txt" /&gt; </code></pre> <p>Or if it is possibly non-existing:</p> <pre class="lang-html prettyprint-override"><code>&lt;c:catch var="e"&gt; &lt;c:import url="/WEB-INF/music/rock/Evanascence/Fallen/text.txt" var="text" /&gt; &lt;/c:catch&gt; &lt;c:choose&gt; &lt;c:when test="${empty e}"&gt; &lt;pre&gt;&lt;c:out value="${text}" /&gt;&lt;/pre&gt; &lt;/c:when&gt; &lt;c:otherwise&gt; &lt;p&gt;An exception occurred when reading file: &lt;pre&gt;${e}&lt;/pre&gt;&lt;/p&gt; &lt;/c:otherwise&gt; &lt;/c:choose&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload