Java - Property (Properties)
About
Key Value - Properties (Property file) in Java.
Properties are values managed as key/value pairs of string datatype. The key identifies, and is used to retrieve, the value.
The implementation of the property class (java.util.Properties) extends java.util.Hashtable
Articles Related
Type
System
The Java platform itself uses a Properties object to maintain its own configuration.
See the property method of the system class.
Access to system properties can be restricted by the Security Manager.
To set a system property when starting the jvm
java -D<name>=<value>
Application
Properties extends java.util.Hashtable. Some of the methods inherited from Hashtable support the following actions:
- testing to see if a particular key or value is in the Properties object,
- getting the current number of key/value pairs,
- removing a key and its value,
- adding a key/value pair to the Properties list,
- enumerating over the values or the keys,
- retrieving a value by its key, and
- finding out if the Properties object is empty.
Management
Load Property Files
// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();
// create application properties with default
Properties applicationProps = new Properties(defaultProps);
// now load properties
// from last invocation
in = new FileInputStream("appProperties");
applicationProps.load(in);
in.close();
Get
applicationProps.getProperty(String key, String default)
Loop
for (String key: properties.stringPropertyNames()){
System.out.println ("The key ("+key+") has the value ("+properties.getProperty(key)+")";
}