Java Properties Class



Introduction

The Java 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.

  • A property list can contain another property list as its 'defaults', this second property list is searched if the property key is not found in the original property list.

  • This class is thread-safe; multiple threads can share a single Properties object without the need for external synchronization.

Class declaration

Following is the declaration for java.util.Properties class −

public class Properties
   extends Hashtable<Object,Object>

Field

Following are the fields for java.util.Properties class −

protected Properties defaults − This is the property list that contains default values for any keys not found in this property list.

Class constructors

Sr.No. Constructor & Description
1

Properties()

This constructs creates an empty property list with no default values.

2

Properties(int initialCapacity)

This constructs creates an empty property list with no default values, and with an initial size accommodating the specified number of elements without the need to dynamically resize.

3

Properties(Properties defaults)

This constructs creates an empty property list with the specified defaults.

Class methods

Sr.No. Method & Description
1 String getProperty(String key)

This method searches for the property with the specified key in this property list.

2 void list(PrintStream out)

This method prints this property list out to the specified output stream.

3 void load(InputStream inStream)

This method reads a property list (key and element pairs) from the input byte stream.

4 void loadFromXML(InputStream in)

This method loads all of the properties represented by the XML document on the specified input stream into this properties table.

5 Enumeration<?> propertyNames()

This method returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.

6 Object setProperty(String key, String value)

This method calls the Hashtable method put.

7 void store(OutputStream out, String comments)

The method writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.

8 void storeToXML(OutputStream os, String comment)

This method emits an XML document representing all of the properties contained in this table.

9 Set<String> stringPropertyNames()

This method returns a set of keys in this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Hashtable
  • java.util.Object

Getting an Enumeration of Properties Keys Example

The following example shows the usage of java.util.Properties.propertyNames() method.

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties prop = new Properties();

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "15");

      // assign the property names in a enumaration
      Enumeration<?> enumeration = prop.propertyNames();

      // print the enumaration elements
      while(enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());  
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Width
Height
Advertisements