从特定位置检索属性文件

时间:2013-11-04 14:22:11

标签: java properties classpath

如何从classpath加载位于src/main/resources/utility下的属性文件?

2 个答案:

答案 0 :(得分:1)

Properties prop = new Properties();
try {
    // load a properties file
    prop.load(new FileInputStream("src/main/resources/utility/config.properties"));

    // get the property value and print it out
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
    ex.printStackTrace();
}

http://www.mkyong.com/java/java-properties-file-examples/

检索的示例

答案 1 :(得分:0)

getResourceAsStream将找到相对于类路径“root”的文件

initFile - 文件位置:

init文件也可以作为战争的一部分打包。

Properties props = new Properties();
InputStream uin = this.getClass().getResourceAsStream(initFile);
InputStreamReader isr = new InputStreamReader(uin);
BufferedReader in = new BufferedReader(new InputStreamReader(uin));
props.load(uin);