linlinyu

          linlinyu
          posts - 0, comments - 4, trackbacks - 0, articles - 15

          Property 文件讀取

          Posted on 2007-11-25 10:56 linlin yu 閱讀(794) 評論(0)  編輯  收藏 所屬分類: java


          通用java 屬性文件讀取,將xxx.properties放在classpath中就可以運行了

          package com.soyoung.xxx.config;

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

          public class XXXConfig {
              
          private static String default_config = "/xxx.properties";

              
          private static Properties mConfig;
              
          static {
                  mConfig 
          = new Properties();

                  
          try {

                      Class config_class 
          = Class.forName(XXXConfig.class.getName());

                      InputStream is 
          = config_class.getResourceAsStream(default_config);
                      mConfig.load(is);

                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          private void XXXConfig() {
              }


              
          public static String getProperty(String key) {
                  
          return mConfig.getProperty(key);
              }


              
          public static String getProperty(String key, String defaultValue) {
                  String value 
          = mConfig.getProperty(key);
                  
          if (value == null)
                      
          return defaultValue;

                  
          return value;
              }


              
          public static boolean getBooleanProperty(String name, boolean defaultValue) {
                  String value 
          = XXXConfig.getProperty(name);

                  
          if (value == null)
                      
          return defaultValue;

                  
          return (new Boolean(value)).booleanValue();
              }


              
          public static int getIntProperty(String name) {
                  
          return getIntProperty(name, 0);
              }


              
          public static int getIntProperty(String name, int defaultValue) {
                  String value 
          = XXXConfig.getProperty(name);

                  
          if (value == null)
                      
          return defaultValue;

                  
          return (new Integer(value)).intValue();
              }


              
          public static void main(String args[]) {
                  
          // mConfig.setProperty("name", "jianglinyu");
                  System.out.println(XXXConfig.getProperty("name"));
              }

          }



           


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


          網站導航:
           
          主站蜘蛛池模板: 辰溪县| 肥乡县| 策勒县| 贵州省| 延边| 达拉特旗| 富宁县| 吴忠市| 怀仁县| 磐安县| 汕头市| 惠州市| 颍上县| 锡林浩特市| 浦北县| 高陵县| 新宁县| 望城县| 赣州市| 华亭县| 安宁市| 泰州市| 合作市| 永吉县| 阜康市| 中牟县| 晋中市| 巢湖市| 西宁市| 盘锦市| 同仁县| 双江| 睢宁县| 长垣县| 忻城县| 兴业县| 甘孜县| 张掖市| 舒兰市| 新昌县| 榆树市|