开发者

Java - Properties: Add new keys to properties file in run time?

开发者 https://www.devze.com 2023-04-08 22:50 出处:网络
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 w开发者_StackOverflowhile installing my applicatio

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 w开发者_StackOverflowhile 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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消