一、特征
- 继承于Hashtable
- 可以作为配置信息的存储容器,也可以加载配置文件中的配置信息
- 线程同步的,多线程使用安全
二、常用方法
- setProperty(String key, String value)
- getProperty(String key)
- getProperty(String key, String defaultValue)
- load(InputStream is)
三、Test
public class Demo04Properties {
@Test
public void test1(){
Properties pro = new Properties();
pro.setProperty("name", "任玉文");
pro.setProperty("sex", "男");
System.out.println(pro);
System.out.println(pro.getProperty("name"));
System.out.println(pro.getProperty("age", "暂无数据"));
}
@Test
public void test2() throws IOException {
Properties pro = new Properties();
// 配置文件在项目路径下(./ 代表项目路径 ../ 代表向外跳一个路径)
FileInputStream fis = new FileInputStream("./config.properties");
pro.load(fis);
System.out.println(pro);
}
}