一,使用getResourceAsStream()
(1)
public static void loading() throws IOException{
//类名.class 属性
//Class.forName("")
//对象.getClass
LoadProperty loadProperty = new LoadProperty();
InputStream is = loadProperty.getClass().getClassLoader().getResourceAsStream("mongodb.properties");
Properties properties = new Properties();
properties.load(is);
Set<Object> keySet = properties.keySet();
for (Object object : keySet) {
System.out.println(object.toString());
System.out.println(properties.get(object.toString()));
}
}
(2)
public static void loading2() throws IOException{
InputStream is = Class.class.getResourceAsStream("/mongodb.properties");
Properties properties = new Properties();
properties.load(is);
Set<Object> keySet = properties.keySet();
for (Object object : keySet) {
System.out.println(object.toString());
System.out.println(properties.get(object.toString()));
}
}
二,使用java.util.ResourceBundle
public static void loading3(){
ResourceBundle bundle = ResourceBundle.getBundle("mongodb");
Enumeration<String> keys = bundle.getKeys();
while(keys.hasMoreElements()){
String string = keys.nextElement().toString();
System.out.println(string);
System.out.println(bundle.getString(string));
}
}