Java Properties backslash

I am using Java Properties to read a properties file. Everything is working fine, but Properties silently drops the backslashes.

(i.e.)

original: c:\sdjf\slkdfj.jpg

after: c:sdjfslkdfj.jpg

How do I make Properties not do this?

I am using the code prop.getProperty(key)

I am getting the properties from a file, and I want to avoid adding double backslashes


It is Properties.load() that's causing the problem that you are seeing as backslash is used for a special purpose.

The logical line holding all the data for a key-element pair may be spread out across several adjacent natural lines by escaping the line terminator sequence with a backslash character, \.

If you are unable to use CoolBeans's suggestion then what you can do is read the property file beforehand to a string and replace backslash with double-backslash and then feed it to Properties.load()

String propertyFileContents = readPropertyFileContents();

Properties properties = new Properties();
properties.load(new StringReader(propertyFileContents.replace("\\", "\\\\")));

Use double backslashes c:\\sdjf\\slkdfj.jpg

Properties props = new Properties();
props.setProperty("test", "C:\\dev\\sdk\\test.dat");
System.out.println(props.getProperty("test"));    // prints C:\dev\sdk\test.dat

UPDATE CREDIT to @ewh below. Apparently, Windows recognises front slashes. So, I guess you can have your users write it with front slashes instead and if you need backslashes afterwards you can do a replace. I tested this snippet below and it works fine.

Properties props = new Properties();
props.setProperty("test", "C:/dev/sdk/test.dat");
System.out.println(props.getProperty("test"));   // prints C:/dev/sdk/test.dat

Use forward slashes. There is never a need in Java to use a backslash in a filename.


In case you really need a backslash in a properties file that will be loaded (like for a property that is not a file path) put \u005c for each backslash character.

The backslash is treated specially in properties files as indicated in the document provided by @unhillbilly.

@EJP: Backslash is definitely needed if, for example, you wanted to store an NTLM login id in a properties file, where the format is DOMAIN\USERNAME with a backslash. This type of property is not a filename so forward slashes will not work.

Edit: @Max Nanasy: From the document (java.util.Properties load javadoc) mentioned above (emphasis mine)

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'

For me, I always had trouble with backslashes in the properties file (even with double backslash '\\') unless I specified the unicode.


Replace \ with \\ as below:

c:\sdjf\slkdfj.jpg 

to

c:\\sdjf\\slkdfj.jpg