Java 从配置文件读取属性值

川长思鸟来 2022-08-18 15:27 291阅读 0赞

有时候,我们需要动态从文件中读取配置,在线上跑个脚本啥的,可以直接修改文件里面的属性值来改变代码的走向,现在用的是 java.util.Properties 来实现的,具体代码如下:

  1. /** 配置,是仅仅浏览还是浏览并同步更新数据 */
  2. private static String ifSynOrJustViewWmsToGss;
  3. private static String everySheetWaitTime;
  4. private static String excelName;
  5. static {
  6. Properties prop = new Properties();
  7. InputStream in = Object.class.getResourceAsStream("/stockConfig.properties");
  8. try {
  9. prop.load(in);
  10. ifSynOrJustViewWmsToGss = prop.getProperty("ifSynOrJustViewWmsToGss").trim();
  11. everySheetWaitTime = prop.getProperty("everySheetWaitTime").trim();
  12. excelName = prop.getProperty("excelName").trim();
  13. logger.info(String.format("库存同步配置文件读取完毕,ifSynOrJustViewWmsToGss:%s,everySheetWaitTime:%s,excelName:%s", ifSynOrJustViewWmsToGss, everySheetWaitTime, excelName));
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. }

stockConfig.properties文件

  1. # only if this value is "update",program will synchronize data truely
  2. ifSynOrJustViewWmsToGss=view
  3. # when read a excel one sheet,wait for a moment
  4. everySheetWaitTime=3000
  5. excelName=wms.xls

配置文件的位置:

Center

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java读取属性配置文件

    文章开始,让我们先了解一下什么是属性配置文件(properties)。 java的通用属性配置文件,以键值对方式存储信息。 还是给个图吧。 ![这里写图片描述][