Properties file backslash and semicolon

13,123

Solution 1

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.

Solution 2

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.

Share:
13,123

Related videos on Youtube

Bobby Rachel
Author by

Bobby Rachel

Updated on September 15, 2022

Comments

  • Bobby Rachel
    Bobby Rachel over 1 year

    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.

    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?

  • Bobby Rachel
    Bobby Rachel over 10 years
    any solution?? but when i read those values from properties file, it will have extra backslashes rite?
  • Jon Skeet
    Jon Skeet over 10 years
    @BobbyRachel: No, it won't. They're just correct escaping - when you use Properties.load(), they will be unescaped, effectively. The problem is that it didn't have the extra backslashes to start with - you need to fix whatever was creating the original properties file.
  • Jon Skeet
    Jon Skeet over 10 years
    Colons and backslashes don't have to be escaped in values... although backslashes followed by valid escape sequences do.