在我的应用程序中,我尝试将 URL 字符串写入属性文件。当然,由于它是一个 URL,因此它包含一个冒号 ( :
)。
我注意到当我调用obj.setProperty("key","value")
时,它用反斜杠转义冒号,传递 \:
相反。
下面是一些重现该行为的代码:
String url="http://google.co.in";
Properties p=new Properties();
FileOutputStream o=new FileOutputStream("abc.properties");
p.setProperties("testurl",url);
p.store(o,null);
o.close();
为什么会发生这种情况? Properties 类在尝试写入某些字符时是否会自动转义它们?他们是哪些角色?
最佳答案
请参阅Properties类(class)store方法 API。它表示字符 #、!、= 和 :
是用转义反斜杠保存的。
The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.
如果您用 load 读回保存的文件Properties 类中的方法,没有问题。如果没有,您将必须编写自己的自定义代码来在加载时转义这些字符。
关于java - 尝试将冒号字符 (":") 写入属性文件会导致该字符被转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19217035/