hi!
I m using netbeans 6.5.1 i am trying to read a text file from a jsp using the code

BufferedReader input = new BufferedReader(new FileReader("contactus.txt"));

this file "contactus.txt" is placed in the same folder (ie web folder) as the jsp file. but the browser is giving the error
file not found. pls help.....
i m not sure where the txt file should be placed in netbeans project to make it find it or should i try a different way to give url?

Dani AI

Generated

As observed, the original exception came from using a plain filename with a filesystem API: that resolves relative to the JVM/process working directory, not the JSP file location in the web application. Translating a web-path into a filesystem location fixes the immediate FileNotFoundException, but there are more robust options and caveats worth noting for future maintenance.

A portable and recommended approach is to read the resource from the webapp itself using the servlet context resource stream; this works whether the WAR is exploded or not and avoids relying on server working directories. Example pattern (handle a null stream and specify a charset, and close streams properly):

try (InputStream in = getServletContext().getResourceAsStream("/contactus.txt")) {
    if (in == null) {
        // resource not found in WAR
    } else {
        try (BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8"))) {
            // read lines
        }
    }
}

NetBeans notes: place static files under the project Web Pages (web) folder so they get packaged at the WAR root; classpath-style resources belong under WEB-INF/classes (or a JAR in WEB-INF/lib) and are accessed via the classloader. Avoid writing back into the webapp directory — files there are transient across redeploys. Also be aware that methods which return a filesystem path can return null if the container does not expose an actual filesystem view of the WAR.

Quick checklist: verify the file is included in the deployed/exploded WAR, check path leading slash and case sensitivity (Linux), inspect the server's deployed folder, enable logging to print the resolved path or note a null stream, and prefer moving file-reading logic out of JSP into a servlet or helper class. See the Servlet API docs for resource access for details: [ServletContext.getResourceAsStream] (https://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html#getResourceAsStream-java.lang.String-) and the behaviour of real path resolution: [ServletContext.getRealPath] (https://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html#getRealPath-java.lang.String-).

Recommended Answers

All 2 Replies

Welcome memegha123.

getRealPath() translate a local URL into a path name in the local file system.

String path=application.getRealPath("/contactus.txt");
BufferedReader input = new BufferedReader(new FileReader(path));

thanx a lot it seems to work i m no longer getting that exception file not found

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.