Java properties file. Problems with the setProperty () method

I am trying to overwrite an existing property value in a Properties file using the Properties#setProperty() method.

But I get an extra backslash ...

For example, I have the following entry inside a properties file:

 #url to server url=http://192.22.222.222 

and when I try to rewrite the value http://192.22.222.222 new value http://192.33.333.333 , I get the following result: http\://192.33.333.333

i.e. the first gap is not needed. I

Where is the problem?

+4
source share
3 answers

No problems. When you download the file again, you will not see a backslash in the property value. The end of escaping avoids all colons (and probably all equal characters), regardless of whether it is strictly required. (When they are not part of the key, you do not need to avoid them, but this probably makes the code simpler for this.)

As long as you always load the code using one of the Properties.load methods, you should not see any problems.

+4
source

From the doc :

The key contains all characters in the line, starting with the first non-white space and up to, but not including, the first unescaped '=' , ':' or a space character other than the terminator string. All of these key termination characters can be included in the key, avoiding them with the previous backslash character; for example

\:\=

will be the two-character key ":=" .

+2
source

Java properties will remove some characters, such as a colon, when writing a property value. See # store properties .

This function is considered a function because the "Properties" format allows the colon as key / value separators (see the source of the property loading method #)

+1
source

Source: https://habr.com/ru/post/1435860/


All Articles