Most Java application need to use properties at some point, they are used to maintain lists of values in which the key is a String and the value is also a String. In property class you can specify a default property that will be returned if no values is associated with a certain key.

Because Properties are subclass of Hastable, the put and putAll methods can be used in Properties object, however it is not recommended because they allow the input of keys or values that are not Strings. That is why we should use setProperty method because if we call the method which contains a non-String key or value, the call will fall. 

Get Properties

We can use two methods to get value by its key.

1. String getProperty(String key), this method will search 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, are then checked. This method will return null if the property is not found. Here is one example of using this method

package myTestPackage;
import java.util.*;
public class Demo {
public static void main(String[] args) {
Properties prop = new Properties();
// Here we will add some properties
prop.put("Height", "190");
prop.put("Widht", "120");
prop.put("Weight", "86");
//We will print out only two properties
System.out.println("" + prop.getProperty("Height"));
System.out.println("" + prop.getProperty("Weight"));
}
}

The output for this code will be:

190
86

2. String getProperty(String key, String defaultProperty), this method is similar to first method, the only difference is that this method will return the default value argument if the property is not found. Here is an example: 

package myTestPackage;
import java.util.*;
public class Demo {
public static void main(String[] args) {
Properties prop = new Properties();
// Here we will add some properties
prop.put("Height", "190");
prop.put("Widht", "120");
Prop.put("Weight", "86");
//We will print out only two properties
System.out.println("" + prop.getProperty("Height", "190"));
System.out.println("" + prop.getProperty("Widht", "120"));
}
}

The output will be:

190
120

Set Properties

We can use this method to update an existed key-value pair or add a new key-value. Here is an example of setProperties:

package myTestPackage;
import java.util.*;
public class PropertiesDemo {
   public static void main(String[] args) {
       // prints Java Runtime Version before property set
       System.out.print("Previous : ");
       System.out.println(System.getProperty("java.runtime.version"));
       Properties p = System.getProperties();
       p.put("java.runtime.version", "Java Runtime 1.6.0");
       System.setProperties(p);
       // prints Java Runtime Version after property set
       System.out.print("New : ");
       System.out.println(System.getProperty("java.runtime.version"));
   }
}

The output for this example will be:

Previous : 1.8.0_181_b13
New : Java Runtime 1.6.0

Remove Properties

There is also a method to remove key-value. Here is an example

package myTestPackage;
import java.util.*;
public class PropertiesDemo {
   public static void main(String[] args) {
       String key = "user.dir";
       System.out.println(key + " = " + System.getProperty(key));

       // The System.clearProperty() method available in Java 1.5
       System.clearProperty(key);
       System.out.println(key + " = " + System.getProperty(key));
   }
}

The output will be:

user.dir = /home/bgroznica/eclipse-workspace/Work
user.dir = nu

Now we have covered the basics of Java Properties, that is all for this tutorial