zoukankan      html  css  js  c++  java
  • Spring读取properties

    resources下有如下properties文件,modelgid.properties中存着key-value对应关系。

        ResourceBundle bundle = PropertyResourceBundle.getBundle("modelgid");
          
           Enumeration keys = bundle.getKeys();
            while(keys.hasMoreElements()) {
                String model = String.valueOf(keys.nextElement());
                String gid = bundle.getString(model);
                logger.info(model + "=" + gid);
                exportDataToMongo(model, gid, year);
            }

    上方法获取到的model-gid对应关系列表是无序的,

    或者使用如下方法,一行一行按顺序读取。即可获取完整的有序配置文件内容。

    public class PropertiesUtils {
        private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
    
        public static List<String> getProperties(String propertyName) throws IOException{
            InputStream fis = null;
            StringBuffer sbf = null;
            BufferedReader br = null;
            List<String> properties = new ArrayList<>();
            try {
                fis = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertyName);
    
                br = new BufferedReader(new InputStreamReader(fis));
                String line = "";
                while ((line = br.readLine()) != null && !line.equals("")) {
                    properties.add(line);
                }
    
            } finally {
                if (br != null){
                    br.close();
                }
                if (fis != null){
                    fis.close();
                }
            }
    
            return properties;
        }
            try {
                List<String> properties = PropertiesUtils.getProperties("modelgid.properties");
                for (String property:properties){
                    String model = property.split("=")[0];
                    String gid = property.split("=")[1];
                    logger.info(model + "=" + gid);
                    exportDataToMongo(model, gid, year);
                }
            } catch (IOException e) {
                logger.error("getProperties error:" + e.getMessage(), e);
                return;
            }
  • 相关阅读:
    Viewer.js 图片预览插件使用
    SqlServer关于“无法删除数据库 "XXXX",因为该数据库当前正在使用”问题的解决方案
    MySQL数据类型详解
    Node.js安装详细步骤教程(Windows版)
    RGB颜色查询对照表
    HTML加载FLASH(*.swf文件)详解
    Cesium区分单击【LEFT_CLICK】和双击事件【LEFT_DOUBLE_CLICK】
    SpringBoot访问jsp页面
    Servlet详解
    Session的生命同期
  • 原文地址:https://www.cnblogs.com/iiot/p/7978948.html
Copyright © 2011-2022 走看看