简体   繁体   English

.properties文件中的特殊字符(“\\”)

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

I'm developing a Java application that executes on Windows. 我正在开发一个在Windows上执行的Java应用程序。 I have several backslashes ("\\") in a .properties-file. 我在.properties文件中有几个反斜杠(“\\”)。 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 : 我使用Spring注释Value读取属性dir

@Value("${dir}")
protected String dir;

This results in the string 127.0.0.1d$dirdir2dir3 when the property dir is used in the code. 当在代码中使用属性dir时,这将导致字符串127.0.0.1d$dirdir2dir3

I have tried unicode escapes like this: 我试过像这样的unicode转义:

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. 当在代码中使用属性dir时,上面的两个尝试都会产生字符串\\\\127.0.0.1d$dirdir2dir3

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. 我希望在代码中使用属性时将属性dir设置为\\\\127.0.0.1\\d$\\dir\\dir2\\dir3 What shall the .properties-file look like to get this result? .properties文件应该是什么样才能得到这个结果?

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

The backslash escape is meant for compiler to understand that the next character is valid and store the result in a String . 反斜杠转义符用于编译器理解下一个字符有效并将结果存储在String When you type \\\\127.0.0.1\\d$\\dir\\dir2\\dir3 , all the backslashes are escaped except the second one (obviously). 当您键入\\\\127.0.0.1\\d$\\dir\\dir2\\dir3 ,除了第二个(显然)之外,所有反斜杠都会被转义。 Do not use a String object here. 不要在这里使用String对象。 Try with Properties and post your results. 尝试使用“ Properties并发布结果。 I had the same experience and use of Properties worked fine. 我有相同的经验和使用属性工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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