⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertyreader.java

📁 struts+hibernate3的源程序
💻 JAVA
字号:


package com.helpsoft.util;

import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * Util class that will read properties from the WEB-INF/classes/directory
 * or by specifying a URL on the filesystem.
 * Also has a helper method for creating a platform independent URL.
 *
 * @author cao guangxin en Erik-Jan de Wit
 * @created 07 aug 2002
 * @version $Revision: 1.1 $, $Date: 2004/11/12 14:06:44 $
 */
public class PropertyReader {

   /**
    * Retrieve the properties specified by the fileName
    * The property file should be in the WEB-INF/classess directory
    * Suppose you need to get the properties in the
    * web-inf/classes/config/application.properties ,
    * you need to pass the propertyFile: config/application.properties
    *
    * @param propertyFile relative path to a properties file in the WEB-INF/classes directory
    * @return a <code>Properties<code> object based on the input file
    **/
   public static Properties getProperties(String propertyFile) {
      try {
         URL url = getPropertiesURL(propertyFile);
         return getProperties(url);
      }
      catch (Exception e) {
         System.out.println("Error ocurred during properties retrieval");
         System.out.println(e.getMessage());
         return null;
      }
   }

   /**
    * This method will return a platform independent URL to a file
    * in the web-inf/classes direcotry.
    *
    * @param fileName relative path to a properties file in the WEB-INF/classes directory
    * @return a platform independent URL to the xml file.
    */
   public static URL getPropertiesURL(String fileName) {
      try {
         System.out.println("Getting the properties URL");
         URL url = null;
         url = PropertyReader.class.getResource("/" + fileName);
         String s = url.toString();
         System.out.println("Filename of the  properties file is: " + s);
         if (s.indexOf("file://") != -1) {
            int indexOf = s.indexOf("file://") + 6;
            String temp = s.substring(0, indexOf);
            System.out.println("temp = " + temp + " moet zijn file:/");
            url = new URL(temp + "//" + s.substring(indexOf));
            System.out.println("The url is now: " + url);
         }
         return url;
      }
      catch (Exception e) {
         System.out.println("Error ocurred during properties retrieval");
         System.out.println(e.getMessage());
         return null;
      }
   }

   /**
    * Retrieve the properties accesible through the specified URL
    *
    * @param url a reference to a properties file
    * @return a properties file
    **/
   public static Properties getProperties(URL url) {
      try {
         Properties props = new Properties();
         // Check for Solaris compatibility.
         // A // in the file protocol won't be found in Solaris.
         props.load(url.openStream());
         System.out.println("Properties have been loaded: " + props);
         return props;
      }
      catch (Exception e) {
         System.out.println("Error ocurred during properties retrieval");
         System.out.println(e.getMessage());
         return null;
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -