文件里有
username=root
password=root
index=1
username=zhangs
password=123456
index=2
username=lisi
password=123456
index=2 怎么用Properties读出来之后存到map中
username=root
password=root
index=1
username=zhangs
password=123456
index=2
username=lisi
password=123456
index=2 怎么用Properties读出来之后存到map中
//根据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;
}
}
extended by java.util.Dictionary
extended by java.util.Hashtable
extended by java.util.PropertiesAll Implemented Interfaces:
Cloneable, Map, Serializable
用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