夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          package org.siyn.util;

          import java.io.InputStream;
          import java.util.Properties;

          /**
           * <p>
           * 本類用提供一個線程同步的方法,讀取資源文件中的配置信息
           * </p>
           * 
           * 
          @author siyn
           * @date 2008-7-10
           
          */
          public class PropertiesReader
          {
              
          private String     file;
              
              
          private Properties properties;
              
              
          /**
               * 構造 PropertysReader
               * 
          @param {String} path 相對于classes的文件路徑
               
          */
              
          public PropertiesReader(String path)
              {
                  
          this.file = path;
                  
          this.properties = new Properties();
              }
              
              
          /**
               * <p>
               * 本方法根據資源名獲取資源內容
               * <p>
               * 
               * 
          @param {String} key 資源文件內key
               * 
          @param {Stirng} defaultValue 默認值
               * 
               * @reaurn String key對應的資源內容
               
          */
              
          public synchronized String getProperty(String key, String defaultValue)
              {
                  
          try
                  {
                      InputStream in 
          = this.getClass().getClassLoader()
                              .getResourceAsStream(
          this.file);
                      
                      properties.load(in);
                      
                  }
                  
          catch (Exception ex1)
                  {
                      System.out.println(
          "沒有找到資源文件:" + this.file);
                  }
                  
          return properties.getProperty(key, defaultValue);
              }
              
              
          /**
               * <p>
               * 本方法根據資源名獲取資源內容
               * <p>
               * 
               * 
          @param {String} key 資源文件內key
               * 
          @param {Stirng} defaultValue 默認值
               * 
          @param {boolean} isnull 如果配置文件value為空,是否使用默認值
               * 
               * @reaurn String key對應的資源內容
               
          */
              
          public synchronized String getProperty(String key, String defaultValue,boolean isnull)
              {
                  String value 
          = null;
                  value 
          = getProperty(key,defaultValue);
                  
          if(isnull && value == null && "".equals(value.trim()) )
                      value 
          = defaultValue;
                  
          return value;
              }
              
              
          public static void main(String[] args)
              {
                  PropertiesReader preader 
          = new PropertiesReader("log4j.properties");
                  String rootLogger 
          = preader.getProperty("aaa""defaul");
                  System.out.println(rootLogger);
              }
          }
          posted on 2008-07-21 14:48 HUIKK 閱讀(267) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 神农架林区| 凤凰县| 恭城| 利川市| 平昌县| 富裕县| 石首市| 大同市| 察雅县| 鹰潭市| 荔浦县| 松桃| 襄城县| 达日县| 东源县| 瑞安市| 神农架林区| 汽车| 汝州市| 铜梁县| 泰和县| 婺源县| 高密市| 黄梅县| 麟游县| 纳雍县| 长沙市| 临城县| 道真| 鲁山县| 襄汾县| 桂阳县| 博野县| 东方市| 饶河县| 巧家县| 库尔勒市| 邹平县| 邢台市| 临安市| 贵港市|