package com.zyf.day22;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* Properties (配置文件类):主要用于生产配置文件与读取配置文件的信息。
* @author root
*
*/
public class demo4 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//creatProperties();
readProperties();
}
//读取配置文件的信息
public static void readProperties() throws IOException{
//创建Properties对象
Properties properties = new Properties();
//加载配置文件信息到Properties中
properties.load(new FileReader("c:\\person.properties"));
//遍历Properties
Set> entrys = properties.entrySet();
for(Entry entry : entrys){
System.out.println("键:" + entry.getKey() + "值:" + entry.getValue());
}
//修改张三密码
properties.setProperty("张三", "007");
properties.store(new FileWriter("c:\\person.properties"), "文件自述");
}
public static void creatProperties() throws IOException{
//创建Properties
Properties properties = new Properties();
properties.setProperty("张三", "123");
properties.setProperty("李四", "234");
properties.setProperty("王五", "345");
//遍历Properties
Set> entrys = properties.entrySet();
for(Entry entry : entrys){
System.out.println("键:" + entry.getKey() + "值:" + entry.getValue());
}
//使用Properties生产配置文件
//properties.store(new FileOutputStream("c:\\person.properties"), "文件自述");//第一个参数是一个输出流对象,第二个参数是使用一个字符描述这个配置文件信息
properties.store(new FileWriter("c:\\person.properties"), "文件自述");
}
}