• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Locating File in Web App

Folks

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"

Cheers
Angus

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


0
amacfarl
Asked:
amacfarl
  • 4
1 Solution
 
bloodredsunCommented:
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 http://www.jguru.com/faq/view.jsp?EID=416061

0
 
amacfarlAuthor Commented:
THANKS
0
 
bloodredsunCommented:
And here's a great article about loading properties files http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
0
 
bloodredsunCommented:
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 "props.properties")

                InputStream is = getClass().getResourceAsStream("/properties/prop.properties");
            Properties props = new Properties();
            try {
                  props.load (is);
            }catch(IOException IOE){
                  _LOGGER.info( "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.
0
 
bloodredsunCommented:
cheers Angus
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now