Java - 属性:在运行时向属性文件添加新键? [英] Java - Properties: Add new keys to properties file in run time?
问题描述
是否可以在运行时创建新的属性文件并添加键和值?我想在安装我的应用程序时根据用户输入向属性文件添加新密钥.我检查了 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屋!