Update Property file in Java

两盒软妹~` 提交于 2021-02-11 12:24:48

问题


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


回答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



来源:https://stackoverflow.com/questions/50171157/update-property-file-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!