Am 31.08.2017 um 08:12 schrieb Thomas Mortagne: > OK but my use case is that the file may or may not already exist and > if it already exist I need to modify it, not overwrite it.
I see. Then there is a constructor of FileBasedConfigurationBuilder that accepts a boolean allowFailOnInit flag. Setting this flag to true will cause the behavior you are probably after: If the file does not exist, an empty configuration is created. Otherwise, it is loaded from the file. Oliver > > My point is that I did not really needed to care about that in 1.x. > > On Wed, Aug 30, 2017 at 5:49 PM, Oliver Heger > <[email protected]> wrote: >> Hi Thomas, >> >> Am 30.08.2017 um 10:56 schrieb Thomas Mortagne: >>> Here is what I currently do: >>> >>> PropertiesBuilderParameters parameters = new >>> Parameters().properties(); >>> if (file.exists()) { >>> new Parameters().properties().setFile(file); >>> } >>> >>> FileBasedConfigurationBuilder<PropertiesConfiguration> builder = >>> new >>> FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class) >>> .configure(parameters); >>> PropertiesConfiguration properties = builder.getConfiguration(); >>> >>> properties.setProperty("property", "value"); >>> >>> builder.getFileHandler().save(file); >>> >>> but I find it more complex than it should. >> >> one option would be to create the PropertiesConfiguration directly, >> populate it, and then save it using a FileHandler. This would roughly >> look something like the following: >> >> PropertiesConfiguration config = new PropertiesConfiguration(); >> config.addProperty(...); >> >> FileHandler handler = new FileHandler(config); >> File out = new File("union.properties"); >> handler.save(out); >> >> More information can be found in the user's guide in the section about >> file-based configurations [1]. >> >> HTH >> Oliver >> >> [1] >> http://commons.apache.org/proper/commons-configuration/userguide/howto_filebased.html#File_Operations_on_Configurations >> >>> >>> IMO the file handler should not care if the file exist or not by >>> default (empty PropertiesConfiguration as in 1.x if it does not exist) >>> and only fail if asked to in the parameters of the builder. At the >>> very least it should be possible to have some way to tell the builder >>> to not care about not existing file. >>> >>> On Wed, Aug 30, 2017 at 10:37 AM, Thomas Mortagne >>> <[email protected]> wrote: >>>> Hi, >>>> >>>> I trying to move from Commons Configuration 1.x to 2.1.1 and I cannot >>>> figure out how to do something that used to be obvious: creating a >>>> properties file that does not yet exist on the file system. >>>> >>>> In 1.x all I had to do is create a PropertiesConfiguration with a >>>> File, set a few properties and then save. >>>> >>>> In 2.1.1 I cannot find how to configure the build for it to accept a >>>> path to a file that does not exist, I always end up with: >>>> >>>> org.apache.commons.configuration2.ex.ConfigurationException: Could not >>>> locate: >>>> org.apache.commons.configuration2.io.FileLocator@28d79cba[fileName=store.properties,basePath=/media/data/projets/xwiki/src/git/xwiki-commons/xwiki-commons-core/xwiki-commons-job/target/test/jobs/status,sourceURL=,encoding=ISO-8859-1,fileSystem=<null>,locationStrategy=<null>] >>>> at >>>> org.apache.commons.configuration2.io.FileLocatorUtils.locateOrThrow(FileLocatorUtils.java:346) >>>> at >>>> org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:972) >>>> at >>>> org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:702) >>>> >>>> and from what I see in FileHandler sources it not really configurable. >>>> >>>> So did I missed something or am I really supposed to create an empty >>>> file before using the builder ? >>>> >>>> Thanks, >>>> -- >>>> Thomas >>> >>> >>> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> > > > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
