简体   繁体   中英

Special characters (“\”) in .properties-file

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?

你可以使用正斜杠,除了它适用于Windows的原因

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM