如何从classpath加载位于src/main/resources/utility
下的属性文件?
答案 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();
}
检索的示例
答案 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);