Java get set Properties

In this section, you will learn how to modify the set of system properties by adding a new property.

Java get set Properties

Java get set Properties

     

In this section, you will learn how to modify the set of system properties by adding a new property. In the example given below, we have create a new property listed in the 'NewProperties.txt' file i.e java.company=Roseindia Pvt Ltd which is not an actual System Property

Properties prop = new Properties(System.getProperties())- This statement initializes the new properties object, prop with the current set of system properties. 

prop.load(propFile) - This method loads the additional property into prop from the file NewProperties.txt.

System.setProperties(prop)- This method takes the Properties object and add the property into the current system properties. It will change the set of system properties for the current running application.

Here is the code of GetSetProperties.java

import java.io.FileInputStream;
import java.util.Properties;

public class GetSetProperties {
  public static void main(String[] args) throws Exception {
  FileInputStream propFile = new FileInputStream(
   "NewProperties.txt");
  Properties prop = new Properties(System.getProperties());
  prop.load(propFile);
  System.setProperties(prop);
  System.getProperties().list(System.out);
  }
}

On the console, you can see a newly added property:

Download Source Code