Properties
1、Properties类到底是什么?
属性集,class Properties extends HashTable implements Map,本质上就是一个Map集合
2、Properties是一个持久的属性集
所谓持久,就是它具有自己写数据到文件的方法
3、Properties是没有泛型,泛型是固定,String和String
4、Properties类,具有Map接口的一切方法
还具有一些自己特有的方法:
public String getProperty(String key);//类似Map中的get
public Object setProperty(String key, String value);//类似Map中的put
public Set stringPropertyNames();//返回此属性列表中的键集
5、Properties具有持久化到文件的功能
store();//保存数据到文件,实际上不是直接保存到文件中,而是写入流中,由流写到文件中
load();//从文件中加载数据,实际上不是直接加载文件,而是加载流中的数据,而流的数据是从该文件中读取的
//Properties持久化数据到文件中
public static void writePs() throws IOException{
Properties ps = new Properties();
ps.setProperty("iphoneX","8888");
ps.setProperty("xiaomiMax","3888");
ps.setProperty("huaweiMate11","6888");
ps.setProperty("vivoX20","1888");
ps.store(new FileWriter("phones.properties"),"");
}
Properties ps = new Properties();
//从文件中读数据
ps.load(new FileReader("phones.properties"));
System.out.println(ps);