Reading Java Property Groups from a File

Can I read different property groups from a Java file without manual processing?

By “manual” I mean reading the file line by line, determining where the group of properties starts, and then extracting the corresponding key-value pairs. In practice, this means rethinking (most of) the wheel that the Properties.load () method creates.

Essentially, what I'm looking for is an easy way to read multiple property groups from a single file, each group can be identified so that it can be loaded into its own Java properties object.

+4
source share
1 answer

I want to use java.util.Properties , you can use prefixes. In the .properties file:

 group1.key1=valgroup1key1 group2.key1=valgroup2key1 group2.key2=valgroup2key2 

and read them as follows:

 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 to use your own, customizable structured file (such as XML).

+7
source

All Articles