/* * Properties(配置文件类)
* 作用:用于生成配置文件与读取配置文件的信息;基于Hash表来存储的,是无序的;
* 注意事项:
* 1. 如果配置文件出现了中文字符,只能使用字符流来生成配置文件,因为使用字节流生成配置文件的话,使用的是ISO8859-1编码,会出现乱码;
* 2. 如果properties中的内容发生了变化,那么需要重新存储配置文件;
*/
package com.michael.lin;
import java.io.FileNotFoundException;
mport java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
public class Demo04 {
public static void main(String[] args) throws FileNotFoundException, IOException{
readProperties();
readProperties1();
}
//创建配置文件
public static void readProperties() throws FileNotFoundException, IOException{
//创建Properties
Properties properties = new Properties();
//添加属性
properties.setProperty("jinge", "123");
properties.setProperty("michael", "xxx1");
properties.setProperty("kinggle", "3457");
//遍历
PropertiesSet> entrys = properties.entrySet();
for(Entryentry : entrys){
System.out.println(entry.getKey() + ":" + entry.getValue());}
//使用properties产生配置文件 store方法是一个文件对象,第二个参数是描述内容;
//properties.store(new FileOutputStream("C:\\conf.properties"), "配置文件");
properties.store(new FileWriter("c:\\per.properties"), "配置文件");}
//读取配置文件信息
public static void readProperties1() throws FileNotFoundException, IOException{
//创建Properties对象
Properties properties = new Properties();
//加载配置文件信息
properties.load(new FileReader("c:\\conf.properties"));
Set> entrys = properties.entrySet();
for(Entryentry : entrys){
System.out.println("键" + entry.getKey() + "值" + entry.getValue());
}
//修改配置文件
properties.setProperty("jinge", "ginkk007");
//修改配置后,重新生产一个配置文件
properties.store(new FileWriter("c:\\conf.properties"), "你好吗");
}
}