Define dynamic separator for Properties.load in java - 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.

Related

What is the best way to store a common part of Strings in Java?

I have some very similar string like this:
String first = "differentConfig1,config1,config2,config3,config4,config5,config6,config7,config8,config9";
String second = "differentConfig2,config1,config2,config3,config4,config5,config6,config7,config8,config9";
String third = "differentConfig3,config1,config2,config3,config4,config5,config6,config7,config8,config9";
where config1 ... config9 are the same in each string, but differentConfig1, differentConfig2 and differentConfig3 are different
what is the best way to avoid duplicating config1-9 in each string?
(Note that config1-9 are around 1 line long values)
What I have now is:
private String commonConfiguration() {
return "config1,config2,config3,config4,config5,config6,config7,config8,config9"
}
and then the strings are constructed like this:
String first = "differentConfig1," + commonConfiguration();
I was thinking about using variables instead of a function, but I am afraid that a very long variable at the beginning of the function would make it less readable.
Configuration strings are better when stored in files. This is how most systems work too. Ex: Most systems like Kafka or Cassandra have configurations of loging, log file location, addresses etc in such configuration files.
So with this approach you can change these configuration strings as per a different environment as needed without impacting code.
With the use of a properties file, your input would just be a collection of key-value pairs.
firstConfig="differentConfig1,config1,config2,config3,config4,config5,config6,config7,config8,config9"
secondConfig="differentConfig2,config1,config2,config3,config4,config5,config6,config7,config8,config9"
thirdConfig="differentConfig3,config1,config2,config3,config4,config5,config6,config7,config8,config9"
I dont really suggest breaking it further, but if you are sure that they will always have the same commonPrefix, one option is to do something like this:
In your properties file,
firstConfig="differentConfig1"
secondConfig="differentConfig2"
thirdConfig="differentConfig3"
commonPrefix="config1,config2,config3....config9"
And in your code, you read these configuration string and append them. Something like this:
String c1 = Registry.getString("firstConfig")+Registry.getString("commonPrefix")
String c2 = Registry.getString("secondConfig")+Registry.getString("commonPrefix")
String c3 = Registry.getString("thirdConfig")+Registry.getString("commonPrefix")
Note: Properties file just a file that is present in your java project that holds the information of the configurations as a key-value pair.

How to create properties without white spaces between separator using org.apache.commons.configuration.PropertiesConfiguration?

Need to write a properties file using org.apache.commons.configuration.PropertiesConfiguration and it should have a format like variablename=variablevalue without having whitespaces between the separator. Is there a way to achieve that using apache commons-configuration?
My current implementation :
PropertiesConfiguration conf = new PropertiesConfiguration("test.properties");
conf.setProperty("key1","value1");
conf.save();
Result :
key1 = value1
Expected Result :
key1=value1
Docs are here: https://commons.apache.org/proper/commons-configuration/userguide_v1.10/howto_properties.html
Untested but I imagine this does it:
conf.getLayout().setGlobalSeparator("=");

Java Properties File: Useful Case of Empty Property Value?

I am messing with properties-files in Java. I want to know if there are some properties-files that use empty values ('propertyName=\n') and what they are used for?
[Update]
Since some folks are still irritated here is a better description:
A sample properties file might look like:
database.username = john
database.password = johnsSecret
Ok so its simple. Now the question is if someone have seen something like:
anyProperty =
I am just asking if you know a use case that actually happen where you needed to express an empty property value within a property file and why was it.
PS: This is no social experiment, it is to determine if this case is practical needed or if I can use it to express the desire to remove the property when specifying a delta to transform an existing properties file by adding or removing its entries. Alternatively I will use -propertyname to remove any property.
The question is so vague I vote to close for "unclear what you are asking", it's so strange that even seems to be a social experiment to analyse people answers from random generated questions..
Having said that, this code saves and read a properties files with what you are asking for.
//Write
String fileName="fileInTheWild.properties";
Properties prop= new Properties();
OutputStream fos = new FileOutputStream( fileName );
prop.put("propertyName", "\n");
prop.store(fos, "Some Properties");
//Read
prop.load(new FileReader(fileName));
String newLine= prop.getProperty("propertyName");
System.out.println("Line 1" + newLine +"Line 2");

Reading Java property groups from a file

Is it possible to read different property groups from a Java file, without manual processing?
By "manual" I mean reading the file line by line, detecting where the start of a property group is and then extracting the corresponding key-value pairs. In practice, this means reinventing (most of) the wheel that the Properties.load() method constitutes.
Essentially, what I am looking for is an easy way of reading, from a single file, multiple groups of properties, with each group being identifiable, so that it can be loaded in its own Java Properties object.
I you want to use java.util.Properties you can use prefixes. In .properties file:
group1.key1=valgroup1key1
group2.key1=valgroup2key1
group2.key2=valgroup2key2
and read them like this:
class PrefixedProperty extends Properties {
public String getProperty(String group, String key) {
return getProperty(group + '.' + key);
}
}
and using:
/* loading, initialization like for java.util.Properties */
String val = prefixedProperty.getProperty("group1", "key1");
You can also use ini4j with windows ini files.
Another, better way is using own, custom structured file (for example XML).

apache commons configuration loads property until "," character

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

Categories

Resources