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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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