Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple values in java.util.Properties

It seems that java.util.Properties assumes one value per propery key. That is,

foo=1 foo=2 

is not expected,

Is there a class for this kind of multi-value property sheet, which also provides the load method?

like image 546
ClueMinus Avatar asked Sep 16 '09 11:09

ClueMinus


People also ask

How do you write multiple lines in properties file?

You add a slash ("\") to continue the value on the next line.

How read multiple values from properties in spring boot?

We use a singleton bean whose properties map to entries in 3 separate properties files. This is the main class to read properties from multiple properties files into an object in Spring Boot. We use @PropertySource and locations of the files in the classpath. The application.

What is Java Util properties?

The java.util.Properties class is a class which represents a persistent set of properties.The Properties can be saved to a stream or loaded from a stream.Following are the important points about Properties − Each key and its corresponding value in the property list is a string.


2 Answers

Try:

foo=1,2  String[] foos = properties.getProperty("foo").split(","); 
like image 146
Nick Holt Avatar answered Oct 06 '22 09:10

Nick Holt


The java.util.Properties function is pretty limited. If you want support list, you might want try PropertyConfiguration from Apache Commons Configuration,

http://commons.apache.org/configuration/userguide/howto_properties.html#Using_PropertiesConfiguration

With it, you can set any delimiters to your list and it will split for you automatically. You can also do other fancy things in properties file. For example,

foo=item1, item2 bar=${foo}, item3 number=123 

You can retrieve it like this,

Configuration config = new PropertiesConfiguration("your.properties"); String[] items = config.getStringArray("bar"); // return {"item1", "item2", "item3"} int number = config.getInt("number", 456); // 456 is default value 
like image 40
ZZ Coder Avatar answered Oct 06 '22 08:10

ZZ Coder