zoukankan      html  css  js  c++  java
  • java读取配置文件内容

    利用com.typesafe.config包实现

    <dependency>
                <groupId>com.typesafe</groupId>
                <artifactId>config</artifactId>
                <version>1.0.2</version>
            </dependency>

    被读取的配置文件config.properties:

    patrol.interval=5
    push.interval=30
    data = [{ controlNum : 1, loopNum : 1, pointNum : 1, status : 110 },
            { controlNum : 1, loopNum : 1, pointNum : 1, status = 111 }]

    java 工具类:

    import com.typesafe.config.Config;
    import com.typesafe.config.ConfigFactory;
    
    
    public class ServerConfig {
    
        private final static Config config;
    
        static {
            config = ConfigFactory.load("config.properties");
        }
    
        public static Config load() {
            return config;
        }
    }

    调用方式:

    Config serverConfig = ServerConfig.load();
     int  PATROL_INTERVAL = serverConfig.getInt("patrol.interval");

    或者使用Java IO实现

    public class PropertiesReader {
    
        private static final String PROPERTIES_FILE_NAME = "aa.properties";
        
        private static Properties config = null;
        
        static {
            config = readProperties(PROPERTIES_FILE_NAME);
        }
        
        public static String getProperty(String key){
            return config.getProperty(key);
        }
        
        private static Properties readProperties(String fileName){
            InputStream inputStream = WsPropertiesReader.class.getClassLoader().getResourceAsStream(fileName);
            Properties config = new Properties();
              try {
                  config.load(inputStream);
              } catch(IOException e){
                  e.printStackTrace();
              } finally{
                  if (inputStream != null){
                      try {
                         inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                  }
              }
              
              return config;
        }
        
        public static Properties readProperties(InputStream inputStream){
            Properties config = new Properties();
              try {
                  config.load(inputStream);
              } catch(IOException e){
                  e.printStackTrace();
              } finally{
                  if (inputStream != null){
                      try {
                          inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                  }
              }
              
              return config;
        }
    }

    对于配置文件中有嵌套的配置内容,一种方法是,在Spring环境中

    使用EmbeddedValueResolverAware读取配置文件内容

    另外可以自己解析${}的形式

    public class PropertiesReader {
    
        private static final String COMMON_PROPERTIES_FILE_NAME = "common-constants.properties";
        private static final String WEB_PROPERTIES_FILE_NAME = "application.properties";
    
        private static Properties commonConfig = null;
        private static Properties webConfig = null;
    
        static {
            commonConfig = readProperties(COMMON_PROPERTIES_FILE_NAME);
            webConfig = readProperties(WEB_PROPERTIES_FILE_NAME);
        }
    
        public static String getProperty(String key) {
            return commonConfig.getProperty(key);
        }
    
        /**
         * 支持复杂el表达式递归调用
         * @param key
         * @return
         */
        public static String getWebProperty(String key) {
            String value = webConfig.getProperty(key);
            String regex = "\$\{(.*?)\}"; //正则表达式
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(value);
            while(matcher.find()){
                String subProperty = getWebProperty(matcher.group(1));
                value = value.replace("${"+matcher.group(1)+"}",subProperty);
            }
            return value;
        }
    
        private static Properties readProperties(String fileName) {
            InputStream inputStream = PropertiesReader.class.getClassLoader().getResourceAsStream(fileName);
    
            Properties config = new Properties();
            try {
                config.load(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            return config;
        }
    }
  • 相关阅读:
    nginx js、css多个请求合并为一个请求(concat模块)
    Web客户端语言HTML、XHTML和XML相关知识介绍
    正则小略
    你可能不知道的5个功能强大的 HTML5 API
    你须知道的30个CSS选择器 »
    css3 media媒体查询器用法总结
    深入java虚拟机学习 -- 类的加载机制
    ElasticSearch和solr的差别
    idea 使用debugger技巧
    vue学习问题总结(一)
  • 原文地址:https://www.cnblogs.com/winkey4986/p/5726370.html
Copyright © 2011-2022 走看看