在web应用下一般用getResourceAsStream
Class.getResourceAsStream(String path)
path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。
InputStream in = this.getClass().getResourceAsStream("/userList.properties");
Properties prop = new Properties();
prop.load(in);
在一般的java project
Properties prop = new Properties();
try {
prop.load(new FileReader("src/userList.properties"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(prop.getProperty("name"));