java读取配置文件信息

Java代码 复制代码
  1. import java.io.FileInputStream;   
  2. import java.io.FileNotFoundException;   
  3. import java.io.FileOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.util.Properties;   
  7.   
  8. import org.apache.commons.logging.Log;   
  9. import org.apache.commons.logging.LogFactory;   
  10.   
  11.   
  12. /** *//**  
  13. * 读取properties文件  
  14. * @author Qutr  
  15. *  
  16. */  
  17. public class Configuration   
  18. {   
  19.     private static final Log log = LogFactory.getLog(Configuration.class);   
  20.     private Properties propertie;   
  21.     private FileInputStream inputFile;   
  22.     private FileOutputStream outputFile;   
  23.     private String propsFile = "src/com/util/headerProp.properties";//配置文件路径   
  24.     /** *//**  
  25.      * 初始化Configuration类  
  26.      */  
  27.     public Configuration()   
  28.     {   
  29.         propertie = new Properties();   
  30.     }   
  31.        
  32.     /** *//**  
  33.      * 初始化Configuration类  
  34.      * @param filePath 要读取的配置文件的路径+名称  
  35.      */  
  36.     public Configuration(String filePath)   
  37.     {   
  38.         propertie = new Properties();   
  39.         try {   
  40.             inputFile = new FileInputStream(filePath);   
  41.             propertie.load(inputFile);   
  42.             inputFile.close();   
  43.         } catch (FileNotFoundException ex) {   
  44.             System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");   
  45.             ex.printStackTrace();   
  46.         } catch (IOException ex) {   
  47.             System.out.println("装载文件--->失败!");   
  48.             ex.printStackTrace();   
  49.         }   
  50.     }//end ReadConfigInfo()   
  51.        
  52.     /** *//**  
  53.      * 重载函数,得到key的值  
  54.      * @param key 取得其值的键  
  55.      * @return key的值  
  56.      */  
  57.     public String getValue(String key)   
  58.     {   
  59.         if(propertie.containsKey(key)){   
  60.             String value = propertie.getProperty(key);//得到某一属性的值   
  61.             return value;   
  62.         }   
  63.         else    
  64.             return "";   
  65.     }//end getValue()   
  66.        
  67.     /** *//**  
  68.      * 重载函数,得到key的值  
  69.      * @param fileName properties文件的路径+文件名  
  70.      * @param key 取得其值的键  
  71.      * @return key的值  
  72.      */  
  73.     public String getValue(String fileName, String key)   
  74.     {   
  75.         try {   
  76.             String value = "";   
  77.             inputFile = new FileInputStream(fileName);   
  78.             propertie.load(inputFile);   
  79.             inputFile.close();   
  80.             if(propertie.containsKey(key)){   
  81.                 value = propertie.getProperty(key);   
  82.                 return value;   
  83.             }else  
  84.                 return value;   
  85.         } catch (FileNotFoundException e) {   
  86.             e.printStackTrace();   
  87.             return "";   
  88.         } catch (IOException e) {   
  89.             e.printStackTrace();   
  90.             return "";   
  91.         } catch (Exception ex) {   
  92.             ex.printStackTrace();   
  93.             return "";   
  94.         }   
  95.     }//end getValue()   
  96.        
  97.     /** *//**  
  98.      * 清除properties文件中所有的key和其值  
  99.      */  
  100.     public void clear()   
  101.     {   
  102.         propertie.clear();   
  103.     }//end clear();   
  104.        
  105.     /** *//**  
  106.      * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,  
  107.      * 当key不存在时,该key的值是value  
  108.      * @param key 要存入的键  
  109.      * @param value 要存入的值  
  110.      */  
  111.     public void setValue(String key, String value)   
  112.     {   
  113.         propertie.setProperty(key, value);   
  114.     }//end setValue()   
  115.        
  116.     /** *//**  
  117.      * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。  
  118.      * @param fileName 文件路径+文件名称  
  119.      * @param description 对该文件的描述  
  120.      */  
  121.     public void saveFile(String fileName, String description)   
  122.     {   
  123.         try {   
  124.             outputFile = new FileOutputStream(fileName);   
  125.             propertie.store(outputFile, description);   
  126.             outputFile.close();   
  127.         } catch (FileNotFoundException e) {   
  128.             e.printStackTrace();   
  129.         } catch (IOException ioe){   
  130.             ioe.printStackTrace();   
  131.         }   
  132.     }//end saveFile()   
  133.     /** *//**  
  134.      * 通过key得到value  
  135.      * @param key  
  136.      */  
  137.     public String getProp(String key) {   
  138.         String retVal = null;   
  139.         InputStream is = getClass().getResourceAsStream(propsFile);   
  140.         Properties props = new Properties();   
  141.         try {   
  142.             props.load(is);   
  143.             retVal = props.getProperty(key);   
  144.             if(retVal == null)retVal="";   
  145.         } catch (Exception e) {   
  146.             log.error(e.getMessage().toString());   
  147.             return null;   
  148.         }   
  149.         return retVal;   
  150.     }   
  151.        
  152. }//end class ReadConfigInfo  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值