从动态 Web 项目中的 class 打开属性文件

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

我正在为 EE Developer 使用 Eclipse。

我需要从 class 的方法 (DBQuery.java) 访问属性文件 (db.properties)。 class 位于 src 文件夹内的一个包内。 对于属性文件,我尝试了几乎所有我能在网上找到的东西来让它工作,但看起来我做不到。 属性文件位于 WebContent 文件夹内,我将添加用于加载此文件的代码:

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;
    } 
}

所以我的问题是,属性文件放在哪里,加载它的正确方法是什么?

您可以将此属性文件放入 java 包中,例如 com/test 并使用以下内容:

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

希望对您有所帮助。