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

醉酒当歌 提交于 2019-12-23 13:27:42

问题


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

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