0

如何加载位于src/main/resources/utility类路径下的属性文件?

4

2 回答 2

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/检索到的示例

于 2013-11-04T14:25:49.073 回答
0

getResourceAsStream will locate the files relative to the "root" of the classpath

initFile - File Location:

init file can also be packed as a part of war.

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);
于 2013-11-04T14:32:54.397 回答