zoukankan      html  css  js  c++  java
  • java读取.properties配置文件的几种方法

          读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):

    一.通过jdk提供的java.util.Properties类

            此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put、putAll这 两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的。因此java.util.Properties类提 供了getProperty()和setProperty()方法来操作属性文件,同时使用store或save(已过时)来保存属性值(把属性值写 入.properties配置文件)。在使用之前,还需要加载属性文件,它提供了两个方法:load和loadFromXML。

            load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。

            可根据不同的方式来获取InputStream,如:

    1.通过当前类加载器的getResourceAsStream方法获取下载地址  

    Java代码  收藏代码
    1. InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");  

    2.从文件获取

    Java代码  收藏代码
    1. InputStream inStream = new FileInputStream(new File("filePath"));  

    3.也是通过类加载器来获取,和第一种一样

    Java代码  收藏代码
    1. InputStream in = ClassLoader.getSystemResourceAsStream("filePath");  

    4.在servlet中,还可以通过context来获取InputStream

    Java代码  收藏代码
    1. InputStream in = context.getResourceAsStream("filePath");  

    5.通过URL来获取

    Java代码  收藏代码
    1. URL url = new URL("path");  
    2. InputStream inStream = url.openStream();  

    读取方法如下:

    Java代码  收藏代码
    1. Properties prop = new Properties();    
    2. prop.load(inStream);    
    3. String key = prop.getProperty("username");    
    4. //String key = (String) prop.get("username");  

    二.通过java.util.ResourceBundle类读取

            这种方式比使用Properties要方便一些。

    1.通过ResourceBundle.getBundle()静态方法来获取

            ResourceBundle是一个抽象类,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

    Java代码  收藏代码
    1. ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可    
    2. String key = resource.getString("username");  

    2.从InputStream中读取

            获取InputStream的方法和上面一样,不再赘述。

    Java代码  收藏代码
    1. ResourceBundle resource = new PropertyResourceBundle(inStream);  

            注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定, 如:test.properties入在com.mmq包下,则要使用com/mmq/test.properties(通过Properties来获 取)或com/mmq/test(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用test.properties 下载地址   或test即可。

    三.实例

    ResourceLoader.java

    Java代码  收藏代码
    1. package com.bijian.study;  
    2.   
    3. import java.io.File;  
    4. import java.io.FileInputStream;  
    5. import java.util.HashMap;  
    6. import java.util.Map;  
    7. import java.util.Properties;  
    8.   
    9. public final class ResourceLoader {  
    10.   
    11.     private static ResourceLoader loader = new ResourceLoader();  
    12.     private static Map<String, Properties> loaderMap = new HashMap<String, Properties>();  
    13.   
    14.     private ResourceLoader() {  
    15.     }  
    16.   
    17.     public static ResourceLoader getInstance() {  
    18.         return loader;  
    19.     }  
    20.       
    21.     public Properties getPropFromProperties(String fileName) throws Exception {  
    22.           
    23.         Properties prop = loaderMap.get(fileName);  
    24.         if (prop != null) {  
    25.             return prop;  
    26.         }  
    27.         String filePath = null;  
    28.         String configPath = System.getProperty("configurePath");  
    29.   
    30.         if (configPath == null) {  
    31.             filePath = this.getClass().getClassLoader().getResource(fileName).getPath();  
    32.         } else {  
    33.             filePath = configPath + "/" + fileName;  
    34.         }  
    35.         prop = new Properties();  
    36.         prop.load(new FileInputStream(new File(filePath)));  
    37.   
    38.         loaderMap.put(fileName, prop);  
    39.         return prop;  
    40.     }  
    41. }  

    PropertiesUtil.java

    Java代码  收藏代码
    1. package com.bijian.study;  
    2.   
    3. import java.util.Properties;  
    4. import java.util.concurrent.ConcurrentHashMap;  
    5. import java.util.concurrent.ConcurrentMap;  
    6.   
    7. /** 
    8.  * 用ConcurrentMap来缓存属性文件的key-value 
    9.  */  
    10. public class PropertiesUtil {  
    11.       
    12.     private static ResourceLoader loader = ResourceLoader.getInstance();  
    13.     private static ConcurrentMap<String, String> configMap = new ConcurrentHashMap<String, String>();  
    14.     private static final String DEFAULT_CONFIG_FILE = "test.properties";  
    15.   
    16.     private static Properties prop = null;  
    17.   
    18.     public static String getStringByKey(String key, String propName) {  
    19.         try {  
    20.             prop = loader.getPropFromProperties(propName);  
    21.         } catch (Exception e) {  
    22.             throw new RuntimeException(e);  
    23.         }  
    24.         key = key.trim();  
    25.         if (!configMap.containsKey(key)) {  
    26.             if (prop.getProperty(key) != null) {  
    27.                 configMap.put(key, prop.getProperty(key));  
    28.             }  
    29.         }  
    30.         return configMap.get(key);  
    31.     }  
    32.   
    33.     public static String getStringByKey(String key) {  
    34.         return getStringByKey(key, DEFAULT_CONFIG_FILE);  
    35.     }  
    36.   
    37.     public static Properties getProperties() {  
    38.         try {  
    39.             return loader.getPropFromProperties(DEFAULT_CONFIG_FILE);  
    40.         } catch (Exception e) {  
    41.             e.printStackTrace();  
    42.             return null;  
    43.         }  
    44.     }  
    45. }  

    Constant.java

    Java代码  收藏代码
    1. package com.bijian.study;  
    2.   
    3. public class Constant {  
    4.       
    5.     public static final String TEST = PropertiesUtil.getStringByKey("test""test.properties");  
    6. }  

    Main.java

    Java代码  收藏代码
    1. package com.bijian.study;  
    2.   
    3. public class Main {  
    4.   
    5.     public static void main(String[] args) {  
    6.           
    7.         System.out.println(Constant.TEST);  
    8.     }  
    9. }  
  • 相关阅读:
    fescar中文官网
    mybatis 中的 update 返回值你真的明白吗
    数据库读写分离搭建
    git 回退各种场景操作
    听说noip2015有幻方
    noi2015的回忆和教训
    bzoj4026
    bzoj4127
    bzoj2119
    关于fft的一点总结
  • 原文地址:https://www.cnblogs.com/s3189454231s/p/5626557.html
Copyright © 2011-2022 走看看