java學習

          java學習

           

          讀取配置文件.properties中的數據

          .properties文件中的數據是鍵值對形式的,key = value格式,把此文件放在緊跟在src目錄下,新建一個類來讀取數據,例如:

          public class ReadCommand {
           /**
            * 讀取properties文件
            */

           private static ReadCommand readConfig = new ReadCommand();
           public static Map<String, String> nodeMap = new HashMap<String, String>();
           
           static{
            System.out.println("ReadConfig...");
            InputStream in = ReadCommand.class.getClassLoader().getResourceAsStream("light_command.properties");
            Properties prop = new Properties();
            try {
             prop.load(in);
             Enumeration en = prop.propertyNames();
             while(en.hasMoreElements()){
              String key = (String) en.nextElement();
              String value = (String) prop.get(key);
              nodeMap.put(key, value);
             }
            } catch (IOException e) {
             e.printStackTrace();
            }
           }
           
           //私有化構造函數
           private ReadCommand(){}
           
           /**
            * 實例化該類(單例)
            *  *  */
           public static ReadCommand getInstance(){
            return readConfig;
           }
           
           /**
            * 獲取配置的節點的信息
            * 
            *   */
           public Map<String, String> getNodes(){
            return nodeMap;
           }
          public static Map<String,String> getLightName(){
            Map<String, String> map = ReadConfig.getInstance().getNodes();
           
           return map;
           }
          Map<String,String> map = GetLightName.getLightName();
            Set<String> keys = map.keySet();//得到鍵值
            for(String key : keys){
             System.out.println(key+"-----"+map.get(key));
            }
          }

          posted on 2012-12-11 11:31 楊軍威 閱讀(276) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 贡嘎县| 乃东县| 尼玛县| 贵阳市| 凌海市| 荥经县| 霍邱县| 芦山县| 晋中市| 吉安县| 金堂县| 阿克陶县| 紫阳县| 闻喜县| 改则县| 廊坊市| 和顺县| 湾仔区| 闽清县| 长岭县| 光泽县| 拜泉县| 兴城市| 凤城市| 岑巩县| 中江县| 乾安县| 克拉玛依市| 交口县| 白沙| 新邵县| 海阳市| 阿巴嘎旗| 舟曲县| 博兴县| 隆德县| 德清县| 固始县| 新昌县| 满洲里市| 酒泉市|