Locating File in Web App

Posted on 2005-05-17
Last Modified: 2010-04-01

Easy question for you.  I would like to use the below methods to load a properties file. If the file is stored in "tomcat\webapps\prop.txt"


    // Read properties file.
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream(""));
    } catch (IOException e) {
    // Write properties file.
    try { FileOutputStream(""), null);
    } catch (IOException e) {

Question by:amacfarl
    LVL 29

    Accepted Solution

    Well in a servlet (inside doPost or doGet) you can call this to get the path to the current webapp context root

    String path = this.getServletContext().getRealPath("/");

    and then work from there.

    see here

    LVL 2

    Author Comment

    LVL 29

    Expert Comment

    And here's a great article about loading properties files
    LVL 29

    Expert Comment

    For someone beginning in webapps I would recommend that you put the properties file in <webapps>/WEB-INF/classes/properties, as it is then loaded into the claspath as can be accessed like this (where the file is called "")

                    InputStream is = getClass().getResourceAsStream("/properties/");
                Properties props = new Properties();
                try {
                      props.load (is);
                }catch(IOException IOE){
             "error loading props...") ;
                      IOE.printStackTrace() ;

    It's much better to have the properties file inside the webapp rather then outside. Even if you want to share the properties file It makes it more of a pain if you have to move the webapp.
    LVL 29

    Expert Comment

    cheers Angus

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    java cyclic graphs 15 186
    some methods private some are not 8 172
    authenticate preemptively soap ui option 3 87
    squirrelPlay java challenge 40 155
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now