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.
properties file. Instantiate the Properties class. Populate the created Properties object using the put() method. Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.
Create a properties fileRight-click and select Add New Properties File. A new properties file will be added to your project. The new file will be selected and highlighted in the list. Type a name for your properties file, for example, "Properties".
To set properties in a Java Properties instance you use the setProperty() method. Here is an example of setting a property (key - value pair) in a Java Properties object: properties.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With