Java - Property (Properties)

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

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.

To set a system property when starting the jvm

java -D<name>=<value>

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.

4 - Management

4..1 - Load Property Files

// create and load default properties
Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");

// create application properties with default
Properties applicationProps = new Properties(defaultProps);

// now load properties 
// from last invocation
in = new FileInputStream("appProperties");

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

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap