Brandon Loehle
Brandon Loehle

Reputation: 266

Java - Escaped backslashes being taken literally when writing to file

I want to store a URL in a properties file. This is the URL:

jdbc\:sqlserver\://dummydata\\SHARED

When programming this in Java, I obviously need to escape the backslashes. So my code ends up looking like this

properties.setProperty("db", "jdbc\\:sqlserver\\://dummydata\\\\SHARED");

The issue with this is that the properties file is saving the String URL and including the backslashes used for escaping, which is an incorrect URL. I was hoping that Java would interpret the backslashes used for escaping so that only the correct URL is saved. Is there a way to achieve this?

Upvotes: 1

Views: 2109

Answers (3)

Andreas
Andreas

Reputation: 159096

You're correct that a property value with : needs to escape the colons in a .properties text file, but you're not writing that text file directly.

You are giving the value to a Properties object using setProperty(), and presumably writing that to a text file using store(), and the store() method will escape the values as needed for you.

You should give the value you want to Properties, and forget about the encoding rules of the text file. Properties will handle all needed encoding. Since the value you want to give is jdbc:sqlserver://dummydata\SHARED, you write a string literal "jdbc:sqlserver://dummydata\\SHARED"

Example

String db = "jdbc:sqlserver://dummydata\\SHARED";
System.out.println(db); // To see actual string value

Properties properties = new Properties();
properties.setProperty("db", db);
try (FileWriter out = new FileWriter("test.properties")) {
    properties.store(out, null);
}

Output

jdbc:sqlserver://dummydata\SHARED

Content of test.properties

#Tue Jun 11 11:54:24 EDT 2019
db=jdbc\:sqlserver\://dummydata\\SHARED

As you can see, the store() method has escaped the : and \ for you.


If you save the properties as an XML file instead, there's no need to escape anything, and Properties won't.

Example

try (FileOutputStream out = new FileOutputStream("test.xml")) {
    properties.storeToXML(out, null);
}

Content of test.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="db">jdbc:sqlserver://dummydata\SHARED</entry>
</properties>

Upvotes: 3

Marc G. Smith
Marc G. Smith

Reputation: 886

Properties.store() escapes backslashes, there is no way around it. I guess my first question is why is this an issue? Are you reading the file in any other way than using Properties.load(). If not they you don't need to worry about it as the load function will remove the escape characters.

   properties.load(file);
   System.out.println(properties.get("db")); 
   // output: jdbc\:sqlserver\://dummydata\\SHARED

As an aside are you sure you the URL is correct? Shouldn't you be storing it as properties.setProperty("jdbc:sqlserver://dummydata\SHARED")?

Upvotes: 1

Sophie
Sophie

Reputation: 434

In the documentation for load, it says the following:

The method does not treat a backslash character, \, before a non-valid escape character as an error; the backslash is silently dropped. For example, in a Java string the sequence "\z" would cause a compile time error. In contrast, this method silently drops the backslash. Therefore, this method treats the two character sequence "\b" as equivalent to the single character 'b'.

This means that two backslashes will be treated as a single one because it's not a valid escape sequence. Loading this string should work just fine:

C:\\path\\to\\file

Upvotes: -1

Related Questions