public classPropertiesUtil {privateResourceBundle resourceBundle;privateString Path;privateProperties propertie;privateInputStream inputStream;/*//不同包下通过Properties读取
String valueString1=new PropertiesUtil("/com/demo/config/config.properties").getPropertyValue("username");
System.out.println(valueString1);
//当前包下读取
String valueString2=new PropertiesUtil("config.properties").getPropertyValue("username");
System.out.println(valueString2);
String valueString3=new PropertiesUtil("com/demo/config/config",Locale.getDefault()).getProperty("username");
System.out.println(valueString3);*/
/**
* Path文件路径
*
* @param Path
* (Path示例:com/demo/config/config)*/
publicPropertiesUtil(String Path, Locale Locale1) {this.resourceBundle =ResourceBundle.getBundle(Path,
Locale1.getDefault());
}/**
*
* @param Path1 (读取当前包下配置文件:config.properties)
* Path2(读取不同包下配置文件:/com/demo/config/config.properties)*/
publicPropertiesUtil(String Path) {this.Path =Path;
}/**
* 通过Properties方式读取 通过对应key获取相对应value值
*
* @param key键值
* @return value值*/
publicString getPropertyValue(String key) {
String result= "";try{if (key == null || "".equals(key) || "null".equals(key)) {return "";
}
propertie= newProperties();
inputStream= PropertiesUtil.class.getResourceAsStream(Path);
propertie.load(inputStream);
inputStream.close();
result=(String) propertie.getProperty(key);
}catch(IOException e) {
e.printStackTrace();
}returnresult;
}/**
* 通过ResourceBundle方式读取 通过对应key获取相对应value值
*
* @param key键值
* @return value值*/
publicString getProperty(String key) {if (key == null || "".equals(key) || "null".equals(key)) {return "";
}
String result= "";try{
result=resourceBundle.getString(key);
}catch(MissingResourceException e) {
e.printStackTrace();
}returnresult;
}
}