Java-Properties配置文件

1、读取Properties文件


// 方法一、从工程目录下获取(Properties文件地址不灵活)
InputStream is = new FileInputStream("xx/xx/xx.properties")); 

// 方法二、默认从类(.class文件)所在目录下获取,若以/开头则从类根目录下获取
InputStream is = getClass().getResourceAsStream("xx.properties");

// 方法三、默认从类根目录下获取,不能以/开头
InputStream is = getClass().getClassLoader().getResourceAsStream("xx/xx/xx.properties");

Properties pro = new Properties(); 
pro.load(is);
is.close();

// 循环读取键值对
Iterator<String> it = pro.stringPropertyNames().iterator();
while(it.hasNext()) System.out.println(key+":"+pro.getProperty(it.next()));

2、写入Properties文件


//true表示追加打开
OutputStream os= new FileOutputStream("xx/xx/xx.properties", true);
Properties pro = new Properties();

// 写入键值对
pro.setProperty("key", "value");

// 写入文件,参数二为文件开头的注释
pro.store(os, "xxx");
os.close();

简书: http://www.jianshu.com/p/4d7184397565
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/54299889
GitHub博客:http://lioil.win/2017/01/10/Properties.html
Coding博客:http://c.lioil.win/2017/01/10/Properties.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值