apache commons configuration loads property until "," character - java

I want to load configuration (apache commons configuration) from a properties file. My program is:
PropertiesConfiguration pc = new PropertiesConfiguration("my.properties");
System.out.println(pc.getString("myValue"));
In my.properties I have
myValue=value,
with comma
When I run program the output is value, not value, with comma. Looks like value is loaded until , character.
Any ideas?

That behavior is clearly documented, i.e., that PropertiesConfiguration treats a value with a comma as multiple values allowing things like:
fruit=apples,banana,oranges
to be interpreted sensibly. The fix (from the doc) is to add a backslash to escape the comma, e.g.,
myKey=value\, with an escaped comma

Check Javadoc. You have to setDelimiterParsingDisabled(true) to disable parsing list of properties.

Actually propConfig.setDelimiterParsingDisabled(true) is working, but you must load the config file after this setting, for example:
propConfig = new PropertiesConfiguration();
propConfig.setDelimiterParsingDisabled(true);
propConfig.load(propertiesFile);
Settings won't work if your code like is:
propConfig = new PropertiesConfiguration(propertiesFile);
propConfig.setDelimiterParsingDisabled(true);

PropertiesConfiguration interprets ',' as a value separator.

If you put \ before the ,, you escape it, and you can read the value
Example:
myValue=value\, with comma
You read = value, with comma without problems

Related

Escape special characters in properties file java

I have key, value in properties file like this proj.path=${HOME}/dir. I have both environment variable and directory also with ${HOME}.
In my case I would like to use it as directory path only but when I read this from file it is getting replaced with environment variable value (home/user/dir).
I tried to escape it like proj.path=\\$\\{HOME\\}/dir but in code it is coming like \$\{HOME\}/dir
Required output is ${HOME}/dir.
EDIT:
Prop file:
proj.path=${HOME}/dir,some/dir/dir2
I am accessing in spring like below.
#Value("#{'${proj.path}'.split(',')}")
private List<String> customPaths;
One way of escaping the ${HOME} value is by wrapping the $ character in an expression and changing the type from List<String> to String[].
proj.path=#{'$'}{HOME}/dir,some/dir/dir2
#Value("${proj.path}")
private String[] customPaths;

commons configuration: comma and multikey issue

My requirement was to update a key/value pair property file for which Commons Configuration is used.
But problem is when you save any text using this api it remove space after comma in a value.
If you disable parsing then it create multiple keys of safe name broken by comma :(
PropertiesConfiguration config = new PropertiesConfiguration("prop.properties");
//config.setDelimiterParsingDisabled(true);
config.save();
Expected value (with no truncation of spaces after before comma):
Name = some , Text , for testing
If setDelimiterParsingDisabled is false then below is outout all spaces gone
Name = some,Text,for testing
If that is True then below is output
Name = some
Name = Text
Name = for testing
I need first one with all space intact means key as is...how to do that
I believe both things cannot be achieved so what is id is I changed Delimeter to carrot ^ sign and not it behaving as I like.
So this is answered.

Why Properties.store delimiting with \ for values separating with :

I have a file which contains properties like :
MyKey=value1:value2
I am using Properties.load to load these into a property object and then outputting the values into another file (using Property.store ).
But the new file is delimiting it with \
MyKey=value1\:value2
Why is this happening ?
This happens, because : is like = a reserved char.
Truth = Beauty
Truth:Beauty
Truth :Beauty
All these lines will set the value for the Property with the Key Truth to Beauty
http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)
The write method will escape the : sign to \:. After loading this chars will be removed.

Define dynamic separator for Properties.load in java

I know that properties in the string can be loaded to a java.util.Properties when it is separated by \n as follows.
Is there any way to do the providing a separator dynamically eg : can we use a comma (,) as a separator?
String propertiesString = "#Namal=153\nJanith=253\nSameera=135";
Properties properties = new Properties();
properties.load(new StringReader(propertiesString));
You can tokenize the commas and join them using newlines before loading it into java.util.Properties.
Sample code below uses commons-lang and assumes you're on UNIX newlines (\n)
String propStr = "Monday=1,Tuesday=2,Wednesday=3";
String[] propsArray = StringUtils.split(propStr, ',');
Properties properties = new Properties();
properties.load(new StringReader(StringUtils.join(propsArray, "\n")));
No,you can't do this!And it's not worthy,just do it with a String .
The '\n' is default line separator, and there is a LineReader inner class which define the logic of line-reading operation,I don't think you have a chance to interact with it.
Properties class is an utility designed to load configurations.
Since \n is the newline character, I can only assume that it was chosen to make the property files easily readable.
If your requirement is to get some data stored in a custom format you can always read them and manually parse the configs using Scanner
Or go with a XML config file with a good schema.
BTW what's wrong with using the new line as a separator? I can only assume that you want to use Properties file to load a different kind of a file because for me the format of the property class make perfect sense in terms of usability and readability.

Java properties value ending in backlash

I have a value in a Java .properties file that needs to end in a backlash. The property value should be "\\server\folder\", and I enter the value like so:
name=\\\\server\\folder\\
The trailing backslash is killing whatever property comes on the next line. Am I escaping this incorrectly?
Not sure what the problem is in your case, but this snippet
Properties props = new Properties();
props.load(new FileInputStream("filename.txt"));
System.out.println(props);
Prints
{prop3=val3, prop2=val2\, prop1=val1}
If filename.txt contains
prop1=val1
prop2=val2\\
prop3=val3
Note that a single (or actually, an odd number) of \ in the end of a property line would escape the newline character and things gets messed up.

Categories

Resources