`
feigme
  • 浏览: 153014 次
  • 性别: Icon_minigender_1
  • 来自: 球面世界
社区版块
存档分类
最新评论

Properties读取类

    博客分类:
  • Java
阅读更多
一个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. }  
分享到:
评论
2 楼 caizhongda 2009-06-06  
哥们怎么不写一下中文注释啊..
1 楼 阳光雨露 2009-02-11  
还算可以...

相关推荐

Global site tag (gtag.js) - Google Analytics