• September 3, 2014
  • Core Java
  • Comments Off on Properties class in java

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 javawithease
 */
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 javawithease
 */
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 javawithease
 */
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.

Related Topics:

Collection framework in java.
Collection interfaces in java.
Collection interface in java with example.
Set interface in java with example.
SortedSet interface in java with example.
List interface in java with example.
Map interface in java with example.
Daemon thread in java in java with example.
SortedMap interface in java with example.
Queue interface in java with example.
Deque interface in java with example.
Enumeration interface in java with example.
Collection classes in java with example.
HashSet in java with example.
LinkedHashSet in java with example.
TreeSet in java with example.
ArrayList in java with example.
LinkedList in java with example.
HashMap in java with example.
LinkedHashMap in java with example.
TreeMap in java with example.
PriorityQueue in java with example.
ArrayDeque in java with example.
Abstract classes in collection framework in java with example.
Sorting in java with example.
Comparable interface in java with example.
Comparator interface in java with example.
Properties class in java with example.
Hashtable in java with example.
ListIterator interface in java with example.

 


Comments are closed.