Properties file backslash and semicolon

2019-01-26 19:16发布

I have a java class to write/append into existing properties file. After appending, its replacing all single backslash with double backslash and it places single backslash before every semicolon.

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

after appending:

Url=jdbc\:oracle\:thin\:@//192.168.1.22\:1521/

Workspace=D\:\\RACHEL\\SW\\Antivirus

How to remove these extra backslashes?

4条回答
看我几分像从前
2楼-- · 2019-01-26 19:37

Properties file has their own format. Colon and backslashes are special characters in properties file. So, they have to be escaped. Also take a look at Properties.load() documentation.

If you are using Properties class to write and read the 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.

查看更多
在下西门庆
3楼-- · 2019-01-26 19:47

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

查看更多
祖国的老花朵
4楼-- · 2019-01-26 20:00

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.

  // 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();
查看更多
SAY GOODBYE
5楼-- · 2019-01-26 20:02

The properties file should have 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.

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.

查看更多
登录 后发表回答