java读取.properties配置文件的几种方法

来源:互联网 发布:记录工作时间的软件 编辑:程序博客网 时间:2024/05/17 05:19

读取.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方法获取下载地址   

  1. InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");  

2.从文件获取

  1. InputStream inStream = new FileInputStream(new File("filePath"));  

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

  1. InputStream in = ClassLoader.getSystemResourceAsStream("filePath");  

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

  1. InputStream in = context.getResourceAsStream("filePath");  

5.通过URL来获取

  1. URL url = new URL("path");  
  2. InputStream inStream = url.openStream();  

读取方法如下:

  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后缀名,只需要文件名即可。

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

2.从InputStream中读取

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

  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

  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

  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

  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

  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. }  

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 超市遇到职业打假人怎么办 阿里巴巴碰到职业打假人怎么办 商家遇到职业打假人怎么办 买过期食品不赔怎么办 淘宝卖假货遇到打假师怎么办 网店遇到职业打假人怎么办 职业打假师把我起诉法院怎么办 被职业打假举报了怎么办 车档杆拉不动显示不在p档怎么办 宜人贷还不起了怎么办 买高跟鞋一只脚合适一只脚挤怎么办 脚瘦穿高跟鞋撑不起来怎么办 银川市阅海幼儿园进不去怎么办 考编专业不对口怎么办 北京55中国际部怎么办 初中数学没学好高中怎么办 靴子大了一码怎么办 靴子买大了一码怎么办 马丁靴大了一码怎么办 社保掌上通登录密码忘记怎么办 录微课时忘词怎么办 微课掌上通看不到信息怎么办 五年级学生上课很吵新老师怎么办 跟财务老师吵起来怎么办 qq把微信冻结了怎么办 微信给封号了怎么办 微信久了没登录冻结了怎么办 换手机号了微店怎么办 ai文件置入后都是字怎么办 excel加载项被禁用了怎么办 被期刊网骗了怎么办 发表的文章不想被收录怎么办? 农村长说的眼睛害了怎么办 普通党员不认同领导的决定怎么办 大学读不下去了怎么办 教师因病长期不能上班怎么办 长按win键黑屏了怎么办 巡视组巡视出问题后续怎么办 货运资格证两次没继续教育怎么办 电子注册备案表学信网查不到怎么办 学信网学籍档案没照片怎么办