0

I'm trying to update my database.properties file in my program. However, it seems Java is adding additional character on my properties file.

Here's a snippet of my code.

Properties props = new Properties();

        String propsFileName = "src/resources/properties/" + "database.properties";

        String[] property = new String[4];

        property[0] = "database.properties";
        property[1] = url.getText();
        property[2] = username.getText();
        property[3] = password.getText();

            try {

                FileInputStream configStream = new FileInputStream(propsFileName);
                props.load(configStream);
                configStream.close();

                props.setProperty("jdbc.driverClassName", Commons.driverClassName);
                props.setProperty("jdbc.url", property[1]);
                props.setProperty("jdbc.username", property[2]);
                props.setProperty("jdbc.password", property[3]);
                props.setProperty("jdbc.comment", comments.getText());

                FileOutputStream output = new FileOutputStream(propsFileName);
                props.store(output, null);
                output.close();

            } catch (IOException ex) {
                ex.printStackTrace();
            }

I have this output on my console which is to check if I get the string that I wanted,

Properties = org.postgresql.Driver, jdbc:postgresql://192.168.1.1:1234/db, username, password, null Program Ran on Fri Jun 23 16:00:37 PHT 2017 by user=xngapp234

However, in my database.properties file, I'm getting this output.

jdbc.url=jdbc\:postgresql\://192.168.1.1\:1234/db
jdbc.username=username
jdbc.password=password
jdbc.comment=
jdbc.driverClassName=org.postgresql.Driver

It adds '\' before the ':' which always gives me an error. Any help is appreciated. Thanks!

sruetti
  • 532
  • 2
  • 7
ashlrem
  • 117
  • 1
  • 3
  • 17
  • 1
    In `\:` is the ``\`` not *before* the `:`? – luk2302 Jun 23 '17 at 08:49
  • 1
    Unrelated: your usage of that string array simply doesn't make much sense. It rather confuses your code. It is not good style to use slot 0 as file name, whereas the other 3 slots represent values to print into that thing. If at all, consider using a `Map` instead: the key represents the property name, and the value, well the value. Or just drop that array completely. It doesn't add any value to your current code. – GhostCat Jun 23 '17 at 08:51
  • What is the problem? Why do you think, the added backslash is causing it? – Usagi Miyamoto Jun 23 '17 at 08:53
  • 2
    This is the normal escaping of `:` in java properties files. `props.load` should retrieve the correct values though . – Arnaud Jun 23 '17 at 08:55
  • Hi all. @GhostCat I will consider your suggestion thank you! I just tried to store it on a string instead of getting it directly through .getText(), but still got error. Thank you! – ashlrem Jun 23 '17 at 09:41
  • @Berger so it's normal to have /: in my properties file right? An xml file is reading the .properties file. I can't connect to database when there's /: in it. – ashlrem Jun 23 '17 at 09:43
  • Yes it is normal, as @sruetti says in his answer, the file is meant to be read through the `Properties` methods, that know how things are escaped in the file . – Arnaud Jun 23 '17 at 09:49
  • 1
    So, the real answer: do a bit of research the next time; and study the javadoc of the classes you are using ;-) – GhostCat Jun 23 '17 at 10:46

1 Answers1

2

If you're trying to store and load properties from a Java program, everything is working as supposed! Your code shows that the url is read without the backslashes - just as you stored it.

As Berger writes, this is the normal escaping in java.properties files. This is documented in the Javadoc of Properties.store():

[...] The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded. [...]

If you try to use the Properties class to write a file to be consumed by a non-Java program, you're out of luck as that's not what this class is intended for. You'll need to use another library for that.

The javadoc states

The Properties class represents a persistent set of properties.

The ".properties"-format is just one representation of a set of properties (with an XML format being another).

sruetti
  • 532
  • 2
  • 7