Properties properties = new Properties();
InputStream stream = PropertiesUtil.class.getClassLoader().getResourceAsStream("setting.properties");
properties.load(stream);
方式二:
//获取sourceFile的url
public static URL getResource(String fileName, ClassLoader loader)
throws IOException {
URL resource = loader.getResource(fileName);
if (resource == null) {
throw new IOException("resource not found: " + fileName);
}
return resource;
}
//根据url加载Properties
public static Properties getProperties(URL source) throws IOException {
InputStream input = source.openStream();
try {
Properties p = new Properties();
p.load(input);
return p;
} finally {
input.close();
}
}