Java Properties setProperty(String key,String value) Method



Description

The Java Properties setProperty(String key,String value) method calls the Hashtable method put. Provided for parallelism with the getProperty method. Enforces use of strings for property keys and values. The value returned is the result of the Hashtable call to put.

Declaration

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

public Object setProperty(String key,String value)

Parameters

  • key − the key to be placed into this property list.

  • value − the value corresponding to key.

Return Value

This method returns the previous value of the specified key in this property list, or null if it did not have one.

Exception

NA

Updating a Key of Properties Example

The following example shows the usage of java.util.Properties.setProperty() method. We've created a Properties object and add properties using setProperty and put method and then properties object is printed. Using setProperty() method again, we've updated the properties and printed them.

package com.tutorialspoint;

import java.util.Properties;

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

      // add some properties
      prop.setProperty("Height", "200");
      prop.put("Width", "1500");
         
      // print the list 
      System.out.println("" + prop);

      // change the properties
      prop.setProperty("Width", "15");
      prop.setProperty("Height", "500");

      // print the list 
      System.out.println("" + prop);
   }
}

Output

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

{Height=200, Width=1500}
{Height=500, Width=15}

Adding a Key of Properties Example

The following example shows the usage of java.util.Properties.setProperty() method. We've created a Properties object and add properties using setProperty methods and then properties object is printed. Using setProperty() method again, we've updated the properties and printed them.

package com.tutorialspoint;

import java.util.Properties;

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

      // add some properties
      prop.setProperty("Height", "200");
      prop.setProperty("Width", "1500");
         
      // print the list 
      System.out.println("" + prop);

      // change the properties
      prop.setProperty("Width", "15");
      prop.setProperty("Height", "500");

      // print the list 
      System.out.println("" + prop);
   }
}

Output

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

{Height=200, Width=1500}
{Height=500, Width=15}
java_util_properties.htm
Advertisements