Java Properties Backslash

Java Properties backslash

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("\\", "\\\\")));

Not able to read Properties File using Backward Slash

\ is an Escape character.

forward slash / is used as path separator in Unix environment.

Back slash \ is used as path separator in Windows environment.

So, You need to use \\ or / as path separator. You can not directly use \ in java. Since, it is an escape character.

So,You need to make changes in your properties file to make your program work.
Use either / or \\ as path separator in your properties file.

In your case you want to show as C:\Users\xxx.a\.

So, use C:\\Users\\xxx.a\\ in your properties file to get output as C:\Users\xxx.a\

How to remove the backslash from properties value in java

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.

Java Properties File to Use / Forward Slash in Key

The use of forward slashes will not cause a problem. To understand why, I suggest you read a critique of the syntax used in Java properties that I wrote. In essence, what you need to know is the following:

  • Leaving aside edge cases (comment lines, blank lines and escape sequences), the syntax of a name=value pair permits almost any character (including forward-slashes) in the name.
  • The = can actually be any of the following: (1) = (optionally preceded and/or followed by whitespace); (2) : (optionally preceded and/or followed by whitespace); or (3) just whitespace. So, yes name=value is equivalent to name:value and also to name value.
  • All escape sequences begin with the backslash character. For details of the escape sequences, I suggest you do a Google search for java.util.Properties to find online documentation for that class, and look at the long description of the load(InputStream) method.

How to ignore backslashes appearing in the properties file for characters like : and =

This is by design. Properties files will treat = and : as key/value delimiters.

To make it explicit as to which part is the key and which is the value the '=' and ':' characters, if included in either part, must be escaped.

Consider the following:

Key: somepassword
Value: Xj993a==

Your properties file will look like:

somepassword=Xj993a==

Unfortunately, where is the key and where is the value? The key could be:

  • somepassword with value Xj993a==
  • somepassword=Xj993a with value =
  • somepassword=Xj993a== with empty value

The parsing of this would be ambiguous at best. Now if we escape the '=' characters:

somepassword=Xj993a\=\=

This is now EXPLICITLY clear as to which is the key and which is the value.

This could also easily have been written as:

somepassword:Xj993a\=\=

Please read the documentation of java.util.Properties.load(java.io.Reader) for more information on the escapes allowed and parsing semantics of properties files.



Related Topics



Leave a reply



Submit