zoukankan      html  css  js  c++  java
  • 代码库-读取属性文件中的值

    要求:实现读取属性文件 config.properties 中属性功能,提供参数key, 得到对应的值。

    测试代码:

    public static void main(String[] args) {
        System.out.println(PropertiesUtil.getStringByKey("user"));
    }

    实现代码:

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.util.HashMap;
     4 import java.util.Map;
     5 import java.util.Properties;
     6 
     7 /**
     8  * 单例模式
     9  */
    10 final class ResourceLoader {
    11 
    12     private static ResourceLoader loader = new ResourceLoader();
    13     private static Map<String, Properties> loaderMap = new HashMap<>();
    14 
    15     private ResourceLoader() {
    16     }
    17 
    18     static ResourceLoader getInstance() {
    19         return loader;
    20     }
    21 
    22     Properties getPropFromProperties(String fileName) throws Exception {
    23 
    24         Properties prop = loaderMap.get(fileName);
    25         if (prop != null) {
    26             return prop;
    27         }
    28         prop = new Properties();
    29 
    30         /*
    31             当程序启动时,如果添加了系统变量 configurePath 的配置,那么就会在这个路径下寻找属性文件,
    32             否则就会在当前的classpath 路径下寻找属性文件
    33           */
    34         String configPath = System.getProperty("configurePath");
    35 
    36         if (configPath == null) {
    37             prop.load(this.getClass().getClassLoader().getResourceAsStream(fileName));
    38         } else {
    39             String filePath = configPath + File.separator + fileName;
    40             prop.load(new FileInputStream(new File(filePath)));
    41         }
    42 
    43         loaderMap.put(fileName, prop);
    44         return prop;
    45     }
    46 
    47 }
    import java.util.Properties;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ConcurrentMap;
    
    /**
     * 读取properties文件
     */
    public class PropertiesUtil {
    
        private static ResourceLoader loader = ResourceLoader.getInstance();
        private static ConcurrentMap<String, String> configMap = new ConcurrentHashMap<String, String>();
        private static final String DEFAULT_CONFIG_FILE = "config.properties";
    
    
        /**
         * 从属性文件propName 中读取属性key
         */
        public static String getStringByKey(String key, String propName) {
            Properties prop;
            try {
                prop = loader.getPropFromProperties(propName);
            } catch (Exception e) {
                throw new RuntimeException("属性文件加载失败", e);
            }
            key = key.trim();
            if (!configMap.containsKey(key)) {
                if (prop.getProperty(key) != null) {
                    configMap.put(key, prop.getProperty(key));
                }
            }
            return configMap.get(key);
        }
    
        /**
         * 从默认属性文件 中读取属性key
         */
        public static String getStringByKey(String key) {
            return getStringByKey(key, DEFAULT_CONFIG_FILE);
        }
    
        /**
         * 从属性文件propName 中读取所有属性
         */
        public static Properties getProperties() {
            try {
                return loader.getPropFromProperties(DEFAULT_CONFIG_FILE);
            } catch (Exception e) {
                return null;
            }
        }
    
    }

    原创文章,欢迎转载,转载请注明出处!

  • 相关阅读:
    python-----贴图 和 报错:OSError: image file is truncated (8 bytes not processed)的处理
    springboot集成RabbitMQ
    MySQL数据库设计规范
    腾讯云COS对象存储
    腾讯云OCR图片文字识别
    java基础之 java注释
    centos7下自动备份mysql数据库
    nginx配置ssl证书
    java基础之 控制语句
    js -- 操作sqlite数据库
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/read-properties.html
Copyright © 2011-2022 走看看