A better class to update property files?

后端 未结 8 2006
不思量自难忘°
不思量自难忘° 2020-12-01 10:27

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

8条回答
  •  悲哀的现实
    2020-12-01 10:45

    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.)

提交回复
热议问题