IO流学习总结(下)---配置文件

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);就会把文件内容清空

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值