Open a properties file from a class in a Dynamic Web Project

935 views Asked by At

I'm using Eclipse for EE Developer.

I need to access to a properties file (db.properties) from a class's method (DBQuery.java). The class is located inside a package inside the src folder. For the properties file i tried almost everything that i could find over the net to make it work, but looks like i can't. The properties file is located inside the WebContent folder, and i'll add the code with which i'm trying to load this file:

public class DBQuery {

    public static String create_DB_string(){

        //the db connection string
        String connString = "";

        try{
            Properties props = new Properties();
            FileInputStream fis = new FileInputStream("db.properties");

            props.load(fis);
            fis.close();

                /* creating connString using props.getProperty("String"); */

         }
       catch (Exception e) {
            System.out.println(e.getClass());
        }       
        return connString;
    } 
}

So my question is, where to put the properties file, and which is the correct way to load it?

1

There are 1 answers

1
Jegg On BEST ANSWER

You can put this propertie file within your java package for example com/test and use following:

getClass().getResourceAsStream( "com/test/myfile.propertie");

Hope it helps.