问题
I'm developing a Java application that executes on Windows. I have several backslashes ("\") in a .properties-file. This is what the file looks like:
dir=\\127.0.0.1\d$\dir\dir2\dir3
I read the property dir
using Spring
annotation Value
:
@Value("${dir}")
protected String dir;
This results in the string 127.0.0.1d$dirdir2dir3
when the property dir
is used in the code.
I have tried unicode escapes like this:
dir=\u005C\u005C127.0.0.1\u005Cd$\u005Cdir\u005Cdir2\u005Cdir3
I have also tried backslash as escape like this:
dir=\\\\127.0.0.1\\d$\\dir\\dir2\\dir3
Both of the tries above results in the string \\127.0.0.1d$dirdir2dir3
when the property dir
is used in the code.
I want the property dir
to be set to \\127.0.0.1\d$\dir\dir2\dir3
when the property is used in the code. What shall the .properties-file look like to get this result?
回答1:
You can use forward slashes, beyond reason it works on Windows
回答2:
The backslash escape is meant for compiler to understand that the next character is valid and store the result in a String
. When you type \\127.0.0.1\d$\dir\dir2\dir3
, all the backslashes are escaped except the second one (obviously). Do not use a String
object here. Try with Properties
and post your results. I had the same experience and use of Properties worked fine.
来源:https://stackoverflow.com/questions/38828523/special-characters-in-properties-file