[可用] java读取.properties配置文件的几种方法

来源:互联网 发布:淘宝单刷 编辑:程序博客网 时间:2024/05/16 20:46

原文链接:http://www.cnblogs.com/s3189454231s/p/5626557.html


现在只贴代码:分三步


step1:定义一个工具类读取文件

package com.bijian.study;    import java.io.File;  import java.io.FileInputStream;  import java.util.HashMap;  import java.util.Map;  import java.util.Properties;    public final class ResourceLoader {        private static ResourceLoader loader = new ResourceLoader();      private static Map<String, Properties> loaderMap = new HashMap<String, Properties>();        private ResourceLoader() {      }        public static ResourceLoader getInstance() {          return loader;      }            public Properties getPropFromProperties(String fileName) throws Exception {                    Properties prop = loaderMap.get(fileName);          if (prop != null) {              return prop;          }          String filePath = null;          String configPath = System.getProperty("configurePath");            if (configPath == null) {              filePath = this.getClass().getClassLoader().getResource(fileName).getPath();          } else {              filePath = configPath + "/" + fileName;          }          prop = new Properties();          prop.load(new FileInputStream(new File(filePath)));            loaderMap.put(fileName, prop);          return prop;      }  }  

step2:然后定义一个工具类,根据名字读取属性

 

package com.bijian.study;    import java.util.Properties;  import java.util.concurrent.ConcurrentHashMap;  import java.util.concurrent.ConcurrentMap;    /**  * 用ConcurrentMap来缓存属性文件的key-value  */  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 = "test.properties";        private static Properties prop = null;        public static String getStringByKey(String key, String propName) {          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);      }        public static String getStringByKey(String key) {          return getStringByKey(key, DEFAULT_CONFIG_FILE);      }        public static Properties getProperties() {          try {              return loader.getPropFromProperties(DEFAULT_CONFIG_FILE);          } catch (Exception e) {              e.printStackTrace();              return null;          }      }  }  



step3:下面进行测试

Constant.javapackage com.bijian.study;    public class Constant {            public static final String TEST = PropertiesUtil.getStringByKey("test", "test.properties");  }  Main.javapackage com.bijian.study;    public class Main {        public static void main(String[] args) {                    System.out.println(Constant.TEST);      }  }  


test.properties文件里的内容可以是这样的

test=987654321



那么Main函数打印出来的值为987654321



0 0