java 属性文件反斜杠和分号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17990740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Properties file backslash and semicolon
提问by Bobby Rachel
I have a Java class to write/append into existing properties file. After appending, it's replacing all single backslash with double backslash and it places single backslash before every semicolon.
我有一个 Java 类可以写入/附加到现有的属性文件中。附加后,它将用双反斜杠替换所有单反斜杠,并在每个分号之前放置单反斜杠。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out= response.getWriter();
String systemPath=request.getParameter("SYSTEMPATH");
String deployPath = getServletConfig().getServletContext().getRealPath("/WEB-INF/DB.properties");
InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/DB.properties");
Properties prop = new Properties();
prop.load(stream);
prop.setProperty("Workspace", systemPath);
File file = new File(deployPath);
FileOutputStream fileOut = new FileOutputStream(file);
prop.store(fileOut, "sample properties");
fileOut.close();
}
Before appending:
追加前:
Url=jdbc:oracle:thin:@//192.168.1.22:1521/
Workspace=D:\RACHEL\SW\Antivirus
url=jdbc:oracle:thin:@//192.168.1.22:1521/
工作区=D:\RACHEL\SW\Antivirus
after appending:
附加后:
Url=jdbc:oracle:thin:@//192.168.1.22:1521/
Workspace=D:\\RACHEL\\SW\\Antivirus
url=jdbc:oracle:thin:@//192.168.1.22:1521/
工作区=D:\\RACHEL\\SW\\杀毒软件
How to remove these extra backslashes?
如何删除这些额外的反斜杠?
回答by Jon Skeet
The properties file shouldhave the extra backslashes to start with. In particular, without them you could end up with the wrong data, e.g. if you have d:\foo\new
that wouldn't mean what you expect it to.
属性文件应该有额外的反斜杠开始。特别是,如果没有它们,您最终可能会得到错误的数据,例如,如果您拥有d:\foo\new
这些数据,则它并不意味着您所期望的那样。
The backslashes escape characters which are sensitive in properties files, basically. The colons are unnecessary (as they're not in the key) but they don't do any harm either. The doubling of backslashes for text is entirely beneficial.
基本上,反斜杠转义属性文件中敏感的字符。冒号是不必要的(因为它们不在键中)但它们也不会造成任何伤害。将文本的反斜杠加倍是完全有益的。
This is documented in the Properties
documentation - in particular, look at the store()
method that you're calling.
这在文档中进行了Properties
记录 - 特别是查看store()
您正在调用的方法。
回答by Rohit Jain
Properties file has their own format. Colonand backslashesare special characters in properties file. So, they have to be escaped. Also take a look at Properties.load()
documentation.
属性文件有自己的格式。冒号和反斜杠是属性文件中的特殊字符。所以,他们必须逃脱。也看看属性。load()
文档。
If you are using Properties
class to writeand readthe file, there won't be any problem. But, if you write the properties file using Property
class, and read using some other method, then you would have to handle the escapes manually.
如果您使用Properties
类来写入和读取文件,则不会有任何问题。但是,如果您使用Property
类编写属性文件,并使用其他方法读取,那么您将不得不手动处理转义。
回答by muthukumar
you can retrieve the key and its value and check and it will not vary but in properties file It seems to look with extra slashes
您可以检索密钥及其值并进行检查,它不会改变,但在属性文件中似乎带有额外的斜杠
回答by hram908
I had this same problem, as I was on stackoverflow on another issue. I remember had this code! I hope it helps, at least it is java, but it does escaping an issue in Properties file backslash and semicolon pitfall.
我遇到了同样的问题,因为我在另一个问题上使用 stackoverflow。我记得有这个代码!我希望它有所帮助,至少它是 java,但它确实避免了 Properties 文件反斜杠和分号陷阱中的问题。
// load to store prop
@SuppressWarnings ( "resource" )
PrintWriter pw = new PrintWriter( configFile );
pw.println( "#" + LocalDateTime.now() );
pw.println( "hibernate.connection.username=" + prop.getProperty( "hibernate.connection.username" ) );
pw.println( "hibernate.connection.password=" + prop.getProperty( "hibernate.connection.password" ) );
pw.println( "hibernate.connection.url=" + prop.getProperty( "hibernate.connection.url" ) );
pw.close();