java学习(properties)

public class LearnProperties {
    public static void main(String[] args) throws IOException{
        Properties prop = new Properties();
        FileInputStream fi = new FileInputStream("E:\\mycode\\java\\learningJava\\src\\learnJava\\learn.properties");
        //FileOutputStream fo = new FileOutputStream("E:\\mycode\\java\\learningJava\\src\\learnJava\\learn.properties");

        prop.load(fi);

        System.out.println("name: " + prop.getProperty("name"));
        System.out.println("age: " + prop.getProperty("age", "18"));
        //若配置文件中为有salary这个key,则返回默认值18w
        System.out.println("salary: " + prop.getProperty("salary", "18w"));

        FileOutputStream fo = new FileOutputStream("E:\\mycode\\java\\learningJava\\src\\learnJava\\learn.properties");

        prop.setProperty("salary", "5 yuan");
        //保存回配置文件中
        prop.store(fo,"");

        Properties prop2 = new Properties();
        prop2.load(fi);
        System.out.println("salary: " + prop2.getProperty("salary", "18w"));

        fo.close();

    }

}

FileOutputStream不要紧接着FileInputStream定义,这样会覆盖掉FileInputStream,导致加载到prop中的内容是空

Properties可以方便的对配置文件进行读写操作

配置文件格式为key=value形式,如下为一个配置文件内容

age=22
name=zxye

以等号划分。

Properties类实现了HashTable接口,因此,在Proterties中的key-value是以Hash形式存储起来。

最后输出结果

name: zxye
age: 22
salary: 18w
salary: 5 yuan

参考: http://blog.csdn.net/qinpeng100423/article/details/9125857

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值