ResourceBundle
默认只加载classpath下的配置文件,也就是普通java项目src下的配置文件
要加载其他目录下配置文件,可以先用流加载,再用ResourceBundle读取。
conf.one 也可以写成conf/one 都是指向conf目录下one.peroperties
package p1;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
public class T1 {
public static void main(String[] args) throws IOException {
//加载classpath下的资源文件
ResourceBundle bundle = ResourceBundle.getBundle("conf.one");
Map<Object, Object> map = new HashMap<>();
for(String s:bundle.keySet()){
Object object = bundle.getObject(s);
map.put(s, object.toString());
}
System.out.println(map);
//加载非classpath下的资文件
//String userdir = System.getProperty("user.dir");
//String path=userdir+"/conf1/one.properties";
//BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
//ResourceBundle bundle = new PropertyResourceBundle(bis);
//
//Map<Object, Object> map = new HashMap<>();
//for(String s:bundle.keySet()){
//Object object = bundle.getObject(s);
//map.put(s, object.toString());
//}
//
//System.out.println(map);
//
}
}