Properties读取类

一个properties常用类,以前收藏的
java 代码
 
  1. package cn.feigme.util;  
  2.   
  3. import java.net.URL;  
  4. import java.util.*;  
  5.   
  6. /** 
  7.  * Util class that will read properties from the WEB-INF/classes/directory 
  8.  * or by specifying a URL on the filesystem. 
  9.  * Also has a helper method for creating a platform independent URL. 
  10.  */  
  11. public class PropertyReader {  
  12.   
  13.    /** 
  14.     * Retrieve the properties specified by the fileName 
  15.     * The property file should be in the WEB-INF/classess directory 
  16.     * Suppose you need to get the properties in the 
  17.     * web-inf/classes/config/application.properties , 
  18.     * you need to pass the propertyFile: config/application.properties 
  19.     * 
  20.     * @param propertyFile relative path to a properties file in the WEB-INF/classes directory 
  21.     * @return a <code>Properties<code> object based on the input file 
  22.     **/  
  23.    public static Properties getProperties(String propertyFile) {  
  24.       try {  
  25.          URL url = getPropertiesURL(propertyFile);  
  26.          return getProperties(url);  
  27.       }  
  28.       catch (Exception e) {  
  29.          System.out.println("Error ocurred during properties retrieval");  
  30.          System.out.println(e.getMessage());  
  31.          return null;  
  32.       }  
  33.    }  
  34.   
  35.    /** 
  36.     * This method will return a platform independent URL to a file 
  37.     * in the web-inf/classes direcotry. 
  38.     * 
  39.     * @param fileName relative path to a properties file in the WEB-INF/classes directory 
  40.     * @return a platform independent URL to the xml file. 
  41.     */  
  42.    public static URL getPropertiesURL(String fileName) {  
  43.       try {  
  44.          System.out.println("Getting the properties URL");  
  45.          URL url = null;  
  46.          url = PropertyReader.class.getResource("/" + fileName);  
  47.          String s = url.toString();  
  48.          System.out.println("Filename of the  properties file is: " + s);  
  49.          if (s.indexOf("file://") != -1) {  
  50.             int indexOf = s.indexOf("file://") + 6;  
  51.             String temp = s.substring(0, indexOf);  
  52.             System.out.println("temp = " + temp + " moet zijn file:/");  
  53.             url = new URL(temp + "//" + s.substring(indexOf));  
  54.             System.out.println("The url is now: " + url);  
  55.          }  
  56.          return url;  
  57.       }  
  58.       catch (Exception e) {  
  59.          System.out.println("Error ocurred during properties retrieval");  
  60.          System.out.println(e.getMessage());  
  61.          return null;  
  62.       }  
  63.    }  
  64.   
  65.    /** 
  66.     * Retrieve the properties accesible through the specified URL 
  67.     * 
  68.     * @param url a reference to a properties file 
  69.     * @return a properties file 
  70.     **/  
  71.    public static Properties getProperties(URL url) {  
  72.       try {  
  73.          Properties props = new Properties();  
  74.          // Check for Solaris compatibility.  
  75.          // A // in the file protocol won't be found in Solaris.  
  76.          props.load(url.openStream());  
  77.          System.out.println("Properties have been loaded: " + props);  
  78.          return props;  
  79.       }  
  80.       catch (Exception e) {  
  81.          System.out.println("Error ocurred during properties retrieval");  
  82.          System.out.println(e.getMessage());  
  83.          return null;  
  84.       }  
  85.    }  
  86. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值