Java - Property (Properties)

> Procedural Languages > Java

1 - 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

Advertising

3 - Type

3.1 - 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.

3.2 - 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.

Advertising

4 - Management

4..1 - 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();

4..2 - Get

applicationProps.getProperty(String key, String default)

4..3 - Loop

for (String key: properties.stringPropertyNames()){
     System.out.println ("The key ("+key+") has the value ("+properties.getProperty(key)+")";
}

5 - Documentation / Reference

lang/java/property.txt · Last modified: 2019/07/01 13:57 by gerardnico