表示一个持久的属性集
持久的:可以保存到流中,也可以从流中读取。可以很方便和文件进行交互(存储到文 件中)
是一个Map体系的集合类,是Hashtable的子类
public class PropertiesExample {
public static void main(String[] args) {
//Properties - > property - > prop
Properties properties = new Properties();
properties.put("1","哈哈");
properties.put("2",1);
properties.put("3",new Student(1,"123",12)); //这种类型存储会出现问题
System.out.println(properties);
}
}
方法
Object setProperty(String key, String value) 设置集合的键和值,都是Stri ng类型,底层调用Hashtable方法 put
String getProperty(String key) 使用此属性列表中指定的键搜索属性
Set stringPropertyNames() 从该属性列表中返回一个不可修改的键集,其中键及 其对应的值是字符串
Properties properties = new Properties();
properties.setProperty("name","李四");
System.out.println(properties.getProperty("name"));
Set<String> set = properties.stringPropertyNames();
set.forEach(System.out::println);
public class PropertiesExample3 {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("name","李四");
properties.setProperty("age","88");
properties.setProperty("hobby","抽烟喝酒烫头");
//存储数据 - 写文件 - 输出流
properties.store(new FileOutputStream("src/com/its/db.properties"),"说明xxx");
// properties.store(new FileWriter("src/com/its/db.properties"),"说明xxx");//这段代码对输入汉字友好
Properties news = new Properties();
//读取数据 - 读文件 - 输入流
news.load(new FileInputStream("src/com/its/db.properties"));
//news.load(new FileReader("src/com/its/db.properties"));//这段代码对输出汉字友好
System.out.println(news);
}
}