Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better class to update property files?

Though java.util.properties allows reading and writing properties file, the writing does not preserve the formatting. Not surprising, because it is not tied to the property file.

Is there a PropertyFile class out there -- or some such -- that preserves comments and blank lines and updates property values in place?

like image 860
Miserable Variable Avatar asked Feb 19 '09 15:02

Miserable Variable


People also ask

How do I edit properties file?

Click the File tab. Click Info to view the document properties. To add or change properties, hover your pointer over the property you want to update and enter the information. Note that for some metadata, such as Author, you'll have to right-click on the property and choose Remove or Edit.

How set value in properties file in Java?

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.

How do I change the properties of a jar file?

Use 7zip, right click on jar and with 7z say open archive, then go to property file, double click and open file with notepad, edit it and save. Now when you will close 7z console, it will ask "update archive?" Say yes... That's it.


2 Answers

It doesn't get much better than Apache's Commons Configuration API. This provides a unified approach to configuration from Property files, XML, JNDI, JDBC datasources, etc.

It's handling of property files is very good. It allows you to generate a PropertiesConfigurationLayout object from your property which preserves as much information about your property file as possible (whitespaces, comments etc). When you save changes to the property file, these will be preserved as best as possible.


Sample code:

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader;  import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.PropertiesConfigurationLayout;  public class PropertiesReader {     public static void main(String args[]) throws ConfigurationException, FileNotFoundException {         File file = new File(args[0] + ".properties");          PropertiesConfiguration config = new PropertiesConfiguration();         PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);         layout.load(new InputStreamReader(new FileInputStream(file)));          config.setProperty("test", "testValue");         layout.save(new FileWriter("path\\to\\properties\\file.properties", false));     } } 

See also:

  • http://mvnrepository.com/artifact/commons-configuration/commons-configuration/
  • https://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration2/PropertiesConfigurationLayout.html
like image 72
Il-Bhima Avatar answered Sep 20 '22 14:09

Il-Bhima


The sample code for using the Apache Commons Configuration library contributed by Patrick Boos is unnecessarily complicated. You don't need to use PropertiesConfigurationLayout explicitly unless you require some advanced control over the output. PropertiesConfiguration by itself is sufficient to preserve comments and formatting:

PropertiesConfiguration config = new PropertiesConfiguration("myprops.properties"); config.setProperty("Foo", "Bar"); config.save(); 

(Note: This code works for the existing 1.10 stable version. I have not checked if it works on the 2.0 alpha builds currently available.)

like image 29
John Rix Avatar answered Sep 20 '22 14:09

John Rix