Java - 属性:在运行时向属性文件添加新密钥? [英] Java - Properties: Add new keys to properties file in run time?

查看:90
本文介绍了Java - 属性:在运行时向属性文件添加新密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建新的属性文件并在运行时添加键和值?
我想在安装我的应用程序时根据用户输入向属性文件添加新密钥。我检查了Java Properties类,但它似乎可以设置现有键的值,但不能将新键添加到属性文件。

Is it possible to create a new properties file and add keys and values in run time? I want to add new keys to properties file depending on user input while installing my application. I checked out Java Properties class but it seem it can set values to existing keys but can not add new keys to properties file.

推荐答案

您只需使用当前不存在的密钥调用 setProperty 即可添加新属性。这只会在内存中执行 - 您必须再次调用 store 以将更改反映回文件:

You can add new properties just by calling setProperty with a key which doesn't currently exist. That will only do it in memory though - you'll have to call store again to reflect the changes back to a file:

Properties prop = new Properties();
prop.load(...); // FileInputStream or whatever

prop.setProperty("newKey", "newValue");
prop.store(...); // FileOutputStream or whatever

这篇关于Java - 属性:在运行时向属性文件添加新密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆