/**
* @author roy
* @version 1.0
* created_dt:2020/09/01
* property tool class
*/
import java.io.*;
import java.util.Properties;
public class PropsUtil {
/**
* @author roy
* @version 1.0
* @param String
* @return Property
* created_dt:2020/09/01
*/
public static Properties loadProps(String proFileName){
Properties props = null;
InputStream streamIn = null;
try{
streamIn = Thread.currentThread().getContextClassLoader().getResourceAsStream(proFileName);
if(streamIn == null){
throw new FileNotFoundException(proFileName + "file is not found");
}
// 转一次流格式,解决中文乱码的问题
BufferedReader readerBuff = new BufferedReader(new InputStreamReader(streamIn,"UTF-8"));
props = new Properties();
props.load(readerBuff);
}catch(IOException e){
LogHelper.getInstance().noteLog(PropsUtil.class.getName(),"load properties|error|" + e,"error");
}finally{
if(streamIn != null){
try{
streamIn.close();
}catch(IOException e){
LogHelper.getInstance().noteLog(PropsUtil.class.getName(),"close input|error|" + e,"error");
}
}
}
return props;
}
/**
* @author roy
* @version 1.0
* @param Properties
* @param String
* @return String
* created_dt:2020/09/01
* desc:读取property配置文件,当节点存在没有赋值时,默认值是空字符串(default "")
*/
public static String getString(Properties proObjName,String proKeyName){
return getString(proObjName,proKeyName,"");
}
/**
* @author roy
* @version 1.0
* @param Properties
* @param String
* @param String
* @return String
* created_dt:2020/09/01
*/
public static String getString(Properties proObjName,String proKeyName,String deftValue){
String reStr = deftValue;
if(proObjName.containsKey(proKeyName)){
reStr = proObjName.getProperty(proKeyName);
}
return reStr;
}
/**
* @author roy
* @version 1.0
* @param Properties
* @param String
* @return int
* created_dt:2020/09/01
*/
public static int getInt(Properties proObjName,String proKeyName){
return getInt(proObjName,proKeyName,0);
}
/**
* @author roy
* @version 1.0
* @param Properties
* @param String
* @param int
* @return int
* created_dt:2020/09/01
*/
public static int getInt(Properties proObjName,String proKeyName,int deftValue){
int reInt = deftValue;
if(proObjName.containsKey(proKeyName)){
reInt = CastUtil.castInt(proObjName.getProperty(proKeyName));
}
return reInt;
}
}
关注公众号“自然语言酷”,每周更新,从原生项目到spring boot,后端架构师进步之路。