可以和流相关联的集合对象Properties.
Map
|–Hashtable
|–Properties
Properties:该集合不需要泛型,因为该集合中的键值对都是String类型。
Properties prop = System.getProperties();
// prop.list(System.out);// 目的是控制台。
// 需求是:将jvm的属性信息存储到一个文件中。
prop.list(new PrintStream("java.txt"));
Properties prop = System.getProperties();
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
System.out.println(key + ":" + prop.getProperty(key));
}
// 1,创建Properties(Map)对象
Properties prop = new Properties();
// 2.使用流加载配置文件。
FileInputStream fis = new FileInputStream("c:\\qq.txt");
// 3。使用Properties 对象的load方法将流中数据加载到集合中。
prop.load(fis);
// 遍历该集合
Set<Entry<Object, Object>> entrySet = prop.entrySet();
Iterator<Entry<Object, Object>> it = entrySet.iterator();
while (it.hasNext()) {
Entry<Object, Object> next = it.next();
Object key = next.getKey();
Object value = next.getValue();
}
// 通过键获取指定的值
Object object = prop.get("jack");
System.out.println(object);
// 通过键修改值
prop.setProperty("jack", "888888");
// 将集合中的数据写入到配置文件中。
FileOutputStream fos = new FileOutputStream("c:\\qq.txt");
// 注释:
prop.store(fos, "yes,qq");
fos.close();
fis.close();