Java Properties getProperty() Method



Description

The Java Properties getProperty(String key) method searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.

Declaration

Following is the declaration for java.util.Properties.getProperty() method

public String getProperty(String key)

Parameters

key − the property key.

Return Value

This method returns the value in this property list with the specified key value.

Exception

NA

Java Properties getProperty(String key, String defaultValue) Methods

Description

The Java Properties getProperty(String key) method searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns the default value argument if the property is not found.

Declaration

Following is the declaration for java.util.Properties.getProperty(String key, String defaultValue) method

public String getProperty​(String key, String defaultValue)

Parameters

key − the property key.

Return Value

This method returns the value in this property list with the specified key value.

Exception

NA

Getting a Value Based on Key from a Properties Example

The following example shows the usage of Java Properties getProperty(String key) method to get a value based on a key from a Properties. We've created a Properties object. Then few entries are added. Using getProperty() method, a value is retrieved and printed.

package com.tutorialspoint;

import java.util.Properties;

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

      //populate properties object
      properties.put("1", "tutorials");
      properties.put("2", "point");
      properties.put("3", "is best");

      System.out.println("Properties elements: " + properties);
      System.out.println("Value: " + properties.getProperty("1"));
   }
}

Output

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

Properties elements: {1=tutorials, 2=point, 3=is best}
Value: tutorials

Getting a Default Value Based on Key from a Properties Example

The following example shows the usage of Java Properties getProperty(String key, String defaultValue) method to get a value based on a key from a Properties. We've created a Properties object. Then few entries are added. Using getProperty() method, a value is retrieved and printed. Then we've getting a non-existent value and in turn printed the default value.

package com.tutorialspoint;

import java.util.Properties;

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

      //populate properties object
      properties.put("1", "tutorials");
      properties.put("2", "point");
      properties.put("3", "is best");

      System.out.println("Properties elements: " + properties);
      System.out.println("Value: " + properties.getProperty("1"));
	  System.out.println("Using default value: " + properties.getProperty("4","Welcome"));
   }
}

Output

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

Properties elements: {1=tutorials, 2=point, 3=is best}
Value: tutorials
Using default value: Welcome
java_util_properties.htm
Advertisements