Java 从配置文件读取属性值
有时候,我们需要动态从文件中读取配置,在线上跑个脚本啥的,可以直接修改文件里面的属性值来改变代码的走向,现在用的是 java.util.Properties 来实现的,具体代码如下:
/** 配置,是仅仅浏览还是浏览并同步更新数据 */
private static String ifSynOrJustViewWmsToGss;
private static String everySheetWaitTime;
private static String excelName;
static {
Properties prop = new Properties();
InputStream in = Object.class.getResourceAsStream("/stockConfig.properties");
try {
prop.load(in);
ifSynOrJustViewWmsToGss = prop.getProperty("ifSynOrJustViewWmsToGss").trim();
everySheetWaitTime = prop.getProperty("everySheetWaitTime").trim();
excelName = prop.getProperty("excelName").trim();
logger.info(String.format("库存同步配置文件读取完毕,ifSynOrJustViewWmsToGss:%s,everySheetWaitTime:%s,excelName:%s", ifSynOrJustViewWmsToGss, everySheetWaitTime, excelName));
} catch (IOException e) {
e.printStackTrace();
}
}
stockConfig.properties文件
# only if this value is "update",program will synchronize data truely
ifSynOrJustViewWmsToGss=view
# when read a excel one sheet,wait for a moment
everySheetWaitTime=3000
excelName=wms.xls
配置文件的位置:
还没有评论,来说两句吧...