文件里有 
username=root 
password=root 
index=1 
username=zhangs 
password=123456 
index=2 
username=lisi 
password=123456 
index=2 怎么用Properties读出来之后存到map中 

解决方案 »

  1.   


     //根据key读取value
     public static String readValue(String filePath,String key) {
      Properties props = new Properties();
            try {
             InputStream in = new BufferedInputStream (new FileInputStream(filePath));
             props.load(in);
             String value = props.getProperty (key);
                System.out.println(key+value);
                return value;
            } catch (Exception e) {
             e.printStackTrace();
             return null;
            }
     }
      

  2.   

    Properties 是 Hashtable 的子类,而 Hashtable 实现了 Map,因此 Properties 也就是个 Map,嘿嘿。
      

  3.   

    一楼的代码完成后,得到了每个 key对应的 value值,然后map.put(key,value)就可以啦
      

  4.   

    java.lang.Object
      extended by java.util.Dictionary
          extended by java.util.Hashtable
              extended by java.util.PropertiesAll Implemented Interfaces:
        Cloneable, Map, Serializable 
      

  5.   

    属性文件(.properties)中不能有相同的属性名!楼主的属性文件中存在多个同名属性值,
    用Properties类加载之后只能保存最后一个同名属性的值。不能保存同名属性的多个值的原因在于Properties类的实现方式,
    Properties类是用java.util.Hashtable实现的,
    Hashtable中相同键值的属性值只能保存最后一个。
    可以参考下面的代码public static void main(String args[]) throws IOException {
        Hashtable<String, String> table = new Hashtable<String, String>();
        table.put("username", "root");
        table.put("username", "zhangs");
        
        Iterator<String> keyIterator = table.keySet().iterator();
        while(keyIterator.hasNext()) {
            String key = keyIterator.next();
            System.out.println(key + "=" + table.get(key));
        }
    }打印结果是
    username=zhangs