- 是一个Map集合,键值对集合
- 核心作用:Properties是用来代表属性文件的,可以读写属性文件里的内容
使用Properties读取属性文件里的键值对数据
public static void main(String[] args) throws IOException {
//1.创建Properties对象出来,空容器
Properties properties = new Properties();
System.out.println(properties);
//2.开始加载属性文件中的键值对数据到Properties对象
properties.load(new FileReader("D:\\java\\src\\user.properties"));
System.out.println(properties);
//3.根据键取值
System.out.println(properties.getProperty("张三"));
//4.遍历全部的键和值
Set<String> keys = properties.stringPropertyNames();
for (String key : keys) {
String value = properties.getProperty(key);
System.out.println(key + "---->" + value);
}
}
使用Properties把键值对数据写出到属性文件
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
//1.存储一些键值对数据
properties.setProperty("kk","123456");
properties.setProperty("sq","minmin");
properties.setProperty("ds","hahahha");
//2.把Properties对象的键值对数据存入到属性文件
properties.store(new FileWriter("src/user2.properties")
, "i saved many users!");
}