Properties类(配置文件)

  1. 专门用于读写配置文件的集合类

  1. 配置文件格式:键=值

ps:默认类型为String


常用方法:

1.

load(InputStream inStream)

从输入字节流读取属性列表(键和元素对)

2.

list(PrintStream out)

将此属性列表打印到指定的输出流。

3.

getProperty(String key, String defaultValue)

使用此属性列表中指定的键搜索属性。

4.

setProperty(String key, String value)

致电 Hashtable方法 put 。

5.

store(OutputStream out, String comments)

将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法加载到 Properties表中的格式输出流。


Properties读取文件

首先写好test.properties配件文件内容,如下

使用Properties类完成对test.properties的读取

Properties props=new Properties();
        InputStream is=new FileInputStream("test.properties");
        //load(InputStream inStream)
        //从字节输入流读取属性列表(键和元素对)
        props.load(is);
        props.list(System.out);

运行结果如下:


Properties类写入文件

将上一步读取到的test.properties修改相关配置信息,并写入到test1.properties

 OutputStream os=new FileOutputStream(new File("test1.properties"));

        props.setProperty("name","bbb");
        props.setProperty("age","18");

        props.store(os,"for test");

运行完成之后,在左边的目录上就会出现test1.properties文件

其内容如下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值