CodesJava

Easy learning with example program codes

Properties class in java


Properties class:

Properties class is used to maintain the data in key value form. It takes both key and value as a string. Properties class is a subclass of Hashtable. It provides the methods to store properties in a properties file and to get the properties from the properties file. System.getProperties() returns the all system properties.

Properties class example:

PropertiesTest.java

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
 
/**
 * This class is used to show the simple 
 * Properties class example.
 * @author codesjava
 */
public class PropetiesTest {
  public static void main(String[] args) {
 
	Properties prop = new Properties();
	Set props;
        String str;
 
	//Set the properties value
	prop.setProperty("database", "oracle10g");
	prop.setProperty("username", "system");
	prop.setProperty("password", "oracle");
 
	//Get key set
	props = prop.keySet(); 
 
	//Print the properties
        Iterator iterator = props.iterator();
        while(iterator.hasNext()) {
          str = (String) iterator.next();
          System.out.println("Prpperty: " + str + 
    	  ", Property Value: " + prop.getProperty(str));
       }
 
  }
}

Output:

Prpperty: password, Property Value: oracle
Prpperty: database, Property Value: oracle10g
Prpperty: username, Property Value: system

Download this example.

Properties class example to write properties in a properties file and read it:

PropertiesTest.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
 
/**
 * This class is used to write properties in a properties 
 * file and read it.
 * @author codesjava
 */
public class PropetiesTest {
  public static void main(String[] args) {
 
	Properties prop = new Properties();
	Set props;
        String str;
 
	//Set the properties value
	prop.setProperty("database", "oracle10g");
	prop.setProperty("username", "system");
	prop.setProperty("password", "oracle");
 
	//Save properties to the project root folder
	try {
		OutputStream output = 
			new FileOutputStream("info.properties");
		prop.store(output, null);
		output.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
 
	//Load the properties file
	Properties infoProp = new Properties();
	try {		
		InputStream input = 
			new FileInputStream("info.properties");
		infoProp.load(input);
		input.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
 
	//Get key set
	props = infoProp.keySet(); 
 
	//Print the properties
       Iterator iterator = props.iterator();
       while(iterator.hasNext()) {
         str = (String) iterator.next();
         System.out.println("Prpperty: " + str + 
          ", Property Value: " + infoProp.getProperty(str));
      }
 
  }
}

Output:

Prpperty: password, Property Value: oracle
Prpperty: database, Property Value: oracle10g
Prpperty: username, Property Value: system

Download this example.

Properties class example to get system properties:

PropertiesTest.java

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
 
/**
 * This class is used to get the system properties 
 * using Properties class.
 * @author codesjava
 */
public class PropetiesTest {
  public static void main(String[] args) {
	//Get system properties.
	Properties prop = System.getProperties(); 
	Set props;
        String str;
 
	//Get key set
	props = prop.keySet(); 
 
	//Print the properties
        Iterator iterator = props.iterator();
        while(iterator.hasNext()) {
          str = (String) iterator.next();
          System.out.println("Prpperty: " + str + 
         ", Property Value: " + prop.getProperty(str));
       }
 
  }
}

Output:

Prpperty: java.runtime.name, Property Value: 
Java(TM) SE Runtime Environment
Prpperty: sun.boot.library.path, Property Value: 
C:\Program Files (x86)\Java\jre7\bin
Prpperty: java.vm.version, Property Value: 23.25-b01
Prpperty: java.vm.vendor, Property Value: 
Oracle Corporation
Prpperty: java.vendor.url, Property Value: 
http://java.oracle.com/
Prpperty: path.separator, Property Value: ;
Prpperty: java.vm.name, Property Value: 
Java HotSpot(TM) Client VM
Prpperty: file.encoding.pkg, Property Value: sun.io
Prpperty: user.country, Property Value: IN
Prpperty: user.script, Property Value: 
Prpperty: sun.java.launcher, Property Value: SUN_STANDARD
...

Download this example.
 
Next Topic: Hashtable in java with example.
Previous Topic: Comparator interface in java with example.



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki