Properties
配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后更改,该文件称之为配置文件。
优势: 提高代码的维护性。
Properties继承map是键值对类型 但是集合中的键值对都是String类型 且没有泛型
线程安全的
都是转换成string 如果加入的是其他类型的 就会报类型强转问题
Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息。
没有自定义泛型 所以迭代器都是Object的
Properties: 该类是一个Map的子类,提供了可以快速操作配置文件的方法
load() : 将文件设备数据装载为Map集合数据
get(key): 获取Map中的数据
getProperty()获取Map中的数据特有方法 取指定键对应的值
setProperty("狗剩","234"); 通过键来修改值
store(Writer ,String comment):使用该方法存储时,会带着当时存储的时间。是生成配置文件
获取集合中所有键元素:
Enumeration propertyNames();
//读取配置文件
public static void readProperties() throws IOException{
//创建Properties对象
Properties properties = new Properties();
//加载配置文件信息到Properties中
properties.load(new FileReader("F:\\persons.properties"));
//遍历
Set<Entry<Object, Object>> entrys = properties.entrySet();
for(Entry<Object, Object> entry :entrys){
System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());
}
//修改狗娃的密码
//把修改后的Properties再生成一个配置文件
properties.setProperty("狗娃", "007");
properties.store(new FileWriter("F:\\persons.properties"), "hehe");
}
//保存配置文件文件的信息。
public static void creatProperties() throws IOException{
//创建Properties
Properties properties = new Properties();
// 通过键修改值
properties.setProperty("狗娃", "123");
properties.setProperty("狗剩","234");
properties.setProperty("铁蛋","345");
// 遍历Properties
/*Set<Entry<Object, Object>> entrys = properties.entrySet();
for(Entry<Object, Object> entry :entrys){
System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());
}*/
// 通过键获取指定的值
Object object = properties.get("狗剩");
//使用Properties生产配置文件。
//properties.store(new FileOutputStream("F:\\persons.properties"), "haha"); //第一个参数是一个输出流对象,第二参数是使用一个字符串描述这个配置文件的信息。
properties.store(new FileWriter("F:\\persons.properties"), "hehe");
}
Properties是配置文件 entrySet可以得到里面的数据
Properties要注意的细节:
1. 如果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,
默认使用的是iso8859-1码表进行编码存储,这时候会出现乱码。
2. 如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不会发生变化。
如果键一样的话 后面后替代前面的
load得到的是哪个文件的全部内容 不重名
setProperties如果重名 就只加入最后一个写入的那个 因为setProperties通过键修改值
fileoutputStream写入的时候true就追加
获取记录程序运行次数:
public class Demo6 {
public static void main(String[] args) throws IOException {
int count = 0;
Properties pro = new Properties();
File file = new File("c:\\count.ini");
FileInputStream fis = null;
if (!file.exists()) {
file.createNewFile();
}
fis = new FileInputStream(file);
pro.load(fis);
String str = pro.getProperty("count");
if (str != null) {
count = Integer.parseInt(str);
}
if (count == 3) {
System.out.println("使用次数已到,请付费");
System.exit(0);
}
count++;
System.out.println("欢迎使用本软件" + "你已经使用了:" + count + " 次");
pro.setProperty("count", count + "");
FileOutputStream fos = new FileOutputStream(new File("c:\\count.ini"));
pro.store(fos, "请保护知识产权");
fis.close();
fos.close();
}
}
总结:
如果properties.setProperty(“count”, String.valueOf(count));中键重复了 后面的覆盖前面的
如果fileOutputStream追加true 那么就会输入多个count .getProperty(“count”);如果配置文件中只有一个 那就哪一个 如果重复了count那么会取最后一个
如果FileOutputStream fileOutputStream=new FileOutputStream(file);在properties.load(new FileInputStream(file));前面 那么load永远取不到值
因为new FileOutputStream(file);就会把文件内容清空