Java讀取properties配置文件

          Posted on 2009-06-17 22:38 eric_xu 閱讀(255) 評(píng)論(0)  編輯  收藏 所屬分類: Java

          package sjtu.rfid;

          import java.util.ResourceBundle;

          public class ConfigBundle {
              
          private static ResourceBundle configBundle = null;
              
              
          static{
                  configBundle 
          = ResourceBundle.getBundle("config");
              }
              
              
          public static String getUsername() {
                  String username 
          = null;

                  
          try {
                      username 
          = configBundle.getString("username");
                  } 
          catch (Exception e) {
                      
          return null;
                  }

                  
          return username;
              }
              
              
          public static String getPassword() {
                  String password 
          = null;

                  
          try {
                      password 
          = configBundle.getString("password");
                  } 
          catch (Exception e) {
                      
          return null;
                  }

                  
          return password;
              }
              
              
          public static void main(String[] args){
                  
                  System.out.println(ConfigBundle.getUsername());
                  System.out.println(ConfigBundle.getPassword());
                  
              }
          }

          將XML轉(zhuǎn)為Properties處理
                  Properties prop = new Properties();
                  prop.loadFromXML(ClassLoader.getSystemResourceAsStream(CONFIG));
                  String url 
          = prop.getProperty(URL);
                  String user 
          = prop.getProperty(USER);
                  String password 
          = prop.getProperty(PASSWORD);

          posts - 37, comments - 5, trackbacks - 0, articles - 0

          Copyright © eric_xu

          主站蜘蛛池模板: 桐庐县| 太原市| 丹江口市| 朝阳县| 林口县| 正蓝旗| 桓台县| 沿河| 襄垣县| 太和县| 颍上县| 马关县| 白水县| 民丰县| 察哈| 台州市| 正镶白旗| 清苑县| 古交市| 贵德县| 江津市| 胶州市| 历史| 韶山市| 潜山县| 清水县| 临颍县| 肃北| 巴塘县| 赣州市| 华亭县| 团风县| 和硕县| 牙克石市| 淳化县| 五大连池市| 石渠县| 策勒县| 墨江| 精河县| 天镇县|