转载  Properties读取类 收藏

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

发表于 @ 2008年11月05日 11:59:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:成功的人生至少需要一张跳板 | 新一篇:J2SE API读取Properties文件六种方法

  • 发表评论
  • 评论内容:
  •  
Copyright © bobor_2008
Powered by CSDN Blog