Home Java Java-get-example Java get set Properties
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java get set Properties

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

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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 1, 2008

Ask Questions?    Discuss: Java get set Properties  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com