PJT
PJT

Reputation: 3597

Full path decleration for a file path used in java for jsp web app

I want to be able to have my paths work on any server not just my dev box. Right now I declear the full path of the file name as such on my local drive.

filename = "H:\test\SourceCode\sample\src\file.txt"

try
        {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            line = in.readLine();
            in.close();
        } 
        catch (IOException e) {
             log.error("Exception Message", e);
        }

How can I set the file path so when I create the .war file I can use it on any server. Such as filename = "src/file.txt" (This doesn't work for me)

Upvotes: 1

Views: 1117

Answers (1)

Bozho
Bozho

Reputation: 597342

Two usual ways:

  • getClass().getResourceAsStream("/..") - resolves the path relative to the classpath - that is, WEB-INF/classes (and jar files)
  • getServletContext().getResourceAsStream("/..") resolves the path relative to the webapp root. That is - webapps/applicationname

Upvotes: 3

Related Questions