0

I have to update the value of a tag in property file. I had to persist the layout of the file after updation, so I am using PropertiesConfiguration API form APACHE.

I have done this and functionality is working as expected. Now there was few keys for those vales have backslash() and forward slash(/).When I am updating it gets changed. Backslash get removed and updation and forward slash(/) becomes / this. Below is the sample code which I am using

properties = new PropertiesConfiguration(("Dbconnect - Copy.properties"));
properties.setProperty("ConfigFilePath", "C:\\Amitabh\\Projects\\");
properties.save();
System.out.println("config.properties updated Successfully!!");

Just to know that how I will prevent. Thanks & Regards Amitabh Pandey

6
  • 1
    Are you saying that ConfigFilePath is "C:/Amitabh/Projects/" in the resulting file? Commented May 4, 2018 at 9:06
  • No it is the ConfigFilePath key value.Property file is Dbconnect - Copy.properties. Commented May 4, 2018 at 9:09
  • give an example for such value? Commented May 4, 2018 at 9:11
  • ABCH555466JKKK\BNH/U this one value of one key.When file gets update it will become. ABCH555466JKKKBNH\/U.Means it will remove the \ slash and convert / to \/(two slash one is backslash and one forward slash) Commented May 4, 2018 at 9:13
  • Well that's the expected behavior. If you have / it should be escaped so it escapes it with a backslash and become \/. Otherwise if it just writes / to the properties file next time you read it you will get different string. If you don't like that behaviour you can make your own reader-writer but what java does there is correct according to the properties files specification Commented May 4, 2018 at 9:25

1 Answer 1

1

If you check here https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

You will see that slashes in properties files have special meaning. They are used to escape characters in properties - for example to spread properties across multiple lines. So before writing them to the properties file you should replace them with double slash (effectively becoming \\\\)

If you don't use Java standard properties readers and read/write the file by yourself you won't have such problems. For example you can open it as a text file and just add the property to the end basically overriding the other times it has been seen - as a work around. But better try with double-double (4) slashes

Sign up to request clarification or add additional context in comments.

6 Comments

The string I have given is a random value.it is encrypted password. Now it is being changed by this. Can we avoid this in any other way??
Then before saving it to the property file do replace("\\","\\\\")
Ah that's proper way to do it then. Reading your previous comment
can we not have any method in PropertiesConfiguration class to do the same. Because if I will do replace then the properties file actual format will be lost. That's why I have used PropertiesConfiguration class.
That is the actual format for properties files - with escaping.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.