0

I have read and tried following

and many blog as well but all are for removing from string not for properties file

I am trying to remove \ from properties file value but no luck

Here is my config.properties file

query=select * from users
field=id

and my Java code that is A.java is

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class A {

    public static void main(String[] args) throws IOException {
        String configPath = "/home/arif/util-test/config.properties";
        Properties prop = new Properties();
        FileInputStream configFileInputStream =  new FileInputStream(configPath);
        prop.load(configFileInputStream);
        System.out.println("Property file loaded "+ configPath);
        configFileInputStream.close();
        String query = prop.getProperty("query");
        query += " where " + prop.getProperty("field") + " = 1";
        //query = query.replaceAll("\\\\", "");
        query = query.replace("\\", "");
        prop.replace("query", query);
        prop.store(new FileOutputStream(configPath), null);
        System.out.println("updated query="+ query);
    }

}

and updated config.properties file

#Mon Oct 03 14:34:27 IST 2016
query=select * from users where id \= 1
field=id

while I am expecting following

#Mon Oct 03 14:34:27 IST 2016
query=select * from users where id = 1
field=id

and getting expected output from terminal or cmd, terminal output is

Property file loaded /home/arif/util-test/config.properties
updated query=select * from users where id = 1

your help will be appreciated! thank you

Community
  • 1
  • 1
Arif Khan
  • 5,039
  • 2
  • 16
  • 27
  • @PeterLawrey, Have you read question before marking duplicate? – Arif Khan Oct 03 '16 at 09:25
  • 2
    @PeterLawrey this is not the same subject here, the problem here is the fact that `=` is special character in properties file as it is used to slit the key and the value of a given property such that it will always be escaped with a backslash but it is totally transparent for the end user as calling `getProperty("query")` will return the value without the backslash – Nicolas Filotto Oct 03 '16 at 09:29
  • Why do you want to remove the backslash as it is transparent for you? it is only an implementation detail – Nicolas Filotto Oct 03 '16 at 09:32
  • thank you @NicolasFilotto, I was thinking that I can not execute with `\` but it is working fine when I load it again – Arif Khan Oct 03 '16 at 09:51

1 Answers1

2

You can't remove the use of \ from a properties file as this is how a Properties file is defined to be implemented.

From the Javadoc for Properties

All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,

\:\=

This line

query=select * from users where id = 1

is not valid as it contains two unescaped = characters. The second one needs to be escaped out.

You could implement properties differently to not need this, but it wouldn't be a standard Properties file.

Alternative ways of storing properties are XML, JSON and YAML. I prefer YAML which is generally simpler/cleaner to read.

Spec for Yaml http://www.yaml.org/spec/1.2/spec.html

Java Library for YAML https://bitbucket.org/asomov/snakeyaml This is a good library for configuration. I would start with this.

Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130