java读取properties文档


java读取properties文档

两种方法: 
第一种:用jdk自带的类 
Java代码   收藏代码
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.util.Properties;  
  4.   
  5. import org.apache.log4j.Logger;  
  6.   
  7. /** 
  8.  * 常量配置类,从指定文件读取常量 
  9.  */  
  10. public class ConfigurableContants {  
  11.     private static final Logger logger = Logger.getLogger(ConfigurableContants.class);  
  12.   
  13.     public static Properties properties = new Properties();  
  14.   
  15.     public static void init(String propertyFileName) {  
  16.         InputStream in = null;  
  17.         try {  
  18.               
  19.    //InputStream inStream =new FileInputStream("sys.properties");  
  20.    // this.getClass().getResourceAsStream("/sys.properties");   
  21.    //Test.class.getClassLoader().getResourceAsStream("sys.properties")          
  22.   
  23. in = ConfigurableContants.class.getResourceAsStream(propertyFileName);  
  24.             if (in != null)  
  25.                 properties.load(in);  
  26.         } catch (IOException e) {  
  27.             logger.error("load " + propertyFileName + " into Contants error");  
  28.         } finally {  
  29.             if (in != null) {  
  30.                 try {  
  31.                     in.close();  
  32.                 } catch (IOException e) {  
  33.                     e.printStackTrace(e);  
  34.                 }  
  35.             }  
  36.         }  
  37.     }  
  38.   
  39.     public static String getProperty(String key, String defaultValue) {  
  40.         return properties.getProperty(key, defaultValue);  
  41.     }  
  42.   
  43.     public static Object setProperty(String key, String Value) {  
  44.         return properties.setProperty(key, Value);  
  45.     }  
  46. }  


第二种:用commons-configuration.jar为我们提供的方法 
参考这个方法http://javamy.iteye.com/blog/252619

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值