【转】Java工具类——资源文件解析类PropertiesUtil

来源:互联网 发布:氧气听书软件下载 编辑:程序博客网 时间:2024/06/03 13:50

处理Properties文件的另一更佳解决方案:见http://blog.csdn.net/softwave/article/details/6873227《Java的Properties文件操作类》     (摘者按)

[java] view plain copy
  1. package com.luang.util.properties;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;    
  4. import java.io.FileOutputStream;    
  5. import java.io.IOException;    
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;    
  8. import java.net.URI;  
  9. import java.util.Enumeration;    
  10. import java.util.HashMap;    
  11. import java.util.Map;    
  12. import java.util.Properties;    
  13. import java.util.ResourceBundle;  
  14.   
  15.   
  16. /** 
  17.  *  
  18.  * PropertiesUtil.java 
  19.  * 
  20.  * @desc properties 资源文件解析工具 
  21.  * @author Guoxp 
  22.  * @datatime Apr 7, 2013 3:58:45 PM 
  23.  * 
  24.  */  
  25. public class PropertiesUtil {    
  26.     
  27.     private Properties props;    
  28.     private URI uri;  
  29.       
  30.     public PropertiesUtil(String fileName){    
  31.         readProperties(fileName);    
  32.     }    
  33.     private void readProperties(String fileName) {    
  34.         try {    
  35.             props = new Properties();    
  36.             InputStream fis =getClass().getResourceAsStream(fileName);    
  37.             props.load(fis);    
  38.             uri = this.getClass().getResource("/dbConfig.properties").toURI();  
  39.         } catch (Exception e) {    
  40.             e.printStackTrace();    
  41.         }    
  42.     }    
  43.     /**  
  44.      * 获取某个属性  
  45.      */    
  46.     public String getProperty(String key){    
  47.         return props.getProperty(key);    
  48.     }    
  49.     /**  
  50.      * 获取所有属性,返回一个map,不常用  
  51.      * 可以试试props.putAll(t)  
  52.      */    
  53.     public Map getAllProperty(){    
  54.         Map map=new HashMap();    
  55.         Enumeration enu = props.propertyNames();    
  56.         while (enu.hasMoreElements()) {    
  57.             String key = (String) enu.nextElement();    
  58.             String value = props.getProperty(key);    
  59.             map.put(key, value);    
  60.         }    
  61.         return map;    
  62.     }    
  63.     /**  
  64.      * 在控制台上打印出所有属性,调试时用。  
  65.      */    
  66.     public void printProperties(){    
  67.         props.list(System.out);    
  68.     }    
  69.     /**  
  70.      * 写入properties信息  
  71.      */    
  72.     public void writeProperties(String key, String value) {    
  73.         try {    
  74.         OutputStream fos = new FileOutputStream(new File(uri));    
  75.             props.setProperty(key, value);    
  76.             // 将此 Properties 表中的属性列表(键和元素对)写入输出流    
  77.             props.store(fos, "『comments』Update key:" + key);    
  78.         } catch (Exception e) {    
  79.         e.printStackTrace();  
  80.         }    
  81.     }       
  82.     public static void main(String[] args) {    
  83.         PropertiesUtil util=new PropertiesUtil("src/dbConfig.properties");    
  84.         util.writeProperties("dbtype""MSSQL");    
  85.     }        
  86. }    
转自【http://blog.csdn.net/guoxuepeng123/article/details/8797916】
0 0