java.util.Properties Class

The Properties class represents yet another specialized Hashtable. Instead of being a collection of key−value pairs of any object, it is customized such that both the keys and the values are only supposed to be strings. However, since this is a subclass of Hashtable, you can call the Hashtable methods directly to store other object types. However, this should be avoided so that the listing, loading, and saving methods work as expected.

The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

In this tutorial you can learn about java.util.Properties class and its examples. And also learn how to use java.util.Properties class.

java.util.Properties class Example
/* Save with file name PropertiesExample.java */

import java.util.Properties;
import java.util.Enumeration;

public class PropertiesExample
{
	public static void main(String args[])
	{
		//java.util.Properties DECLARATION
		Properties p;

		//java.util.Properties OBJECT CREATION
		//USING DEFAULT CONSTRUCTOR
		p = new Properties();

		//ADD AN ELEMENTS
		p.setProperty("Property1","Property1 Value");
		p.setProperty("Property2","Property2 Value");
		p.setProperty("Property3","Property3 Value");

		//Properties OUTPUT
		System.out.println("--------------------------------");
		System.out.println("PropertyName\tValue");
		System.out.println("--------------------------------");
		String key = "", value = "";
		Enumeration e = p.propertyNames();
		while(e.hasMoreElements())
		{
			key = (String) e.nextElement();
			value = p.getProperty(key);
			System.out.println(key + "\t" + value);
		}
	}
}
                        
java.util.Properties class Example 2
/* In this example you can learn how to get system properties. */
/* Save with file name PropertiesExample2.java */

import java.util.Properties;
import java.util.Enumeration;

public class PropertiesExample2
{
	public static void main(String args[])
	{
		//java.util.Properties DECLARATION
		Properties p;

		//java.util.Properties OBJECT CREATION
		p = System.getProperties();

		//Properties OUTPUT
		System.out.println(printLine("=",80));
		System.out.printf("%30s | Value\n", " PropertyName");
		System.out.println(printLine("=",80));
		String key = "", value = "";
		Enumeration e = p.propertyNames();
		while(e.hasMoreElements())
		{
			key = (String) e.nextElement();
			value = p.getProperty(key);
			System.out.printf("%30s | " + value, key);
			System.out.println();
			System.out.println();
			System.out.println(printLine("-",80));
		}
	}

	public static String printLine(String c, int len)
	{
			if(c==null || c.compareTo("")==0)
				return "";
			if(len<=0)
				return "";
			String line = "";
			for(int i=0;i<len;i++)
			{
				line += c;
			}
			return line;
	}
}
                        
java.util.Properties class Example 3
/* This example shows how to save Properties into File */
/* Save with file name PropertiesExample3.java */

import java.util.Properties;
import java.util.Enumeration;
import java.io.FileOutputStream;

public class PropertiesExample3
{
	public static void main(String args[])
	{
		try
		{
			//java.util.Properties DECLARATION
			Properties p;

			//java.util.Properties OBJECT CREATION
			p = new Properties();

			p.put("PROMPT", "$p$g");
			p.put("TEMP","C:\\Windows\\Temp");
			p.put("MYAPPNAME", "Huda Tutorials");

			//FileOutputStream CREATION
			FileOutputStream fos = new FileOutputStream("myproperties.txt");

			//SAVE Properties INTO FILE
			p.store(fos, "Environment Settings");

			//CLOSE THE FileOutputStream
			fos.close();
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e);
		}
	}
}
                        
java.util.Properties class Example 4
/* This example shows how to load Properties from File */
/* Save with file name PropertiesExample4.java */

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

public class PropertiesExample4
{
	public static void main(String args[])
	{
		try
		{
			//java.util.Properties DECLARATION
			Properties p;

			//java.util.Properties OBJECT CREATION
			p = new Properties();

			//FileInputStream CREATION
			FileInputStream fis = new FileInputStream("myproperties.txt");

			//LOAD Properties FROM FILE
			p.load(fis);

			//CLOSE THE FileInputStream
			fis.close();

			//DISPLAY RETRIEVED Properties
			p.store(System.out," RETRIEVED Properties");
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e);
		}
	}
}
                        
java.util.Properties class Example 5
/* This example shows how to save Properties into XML File */
/* Save with file name PropertiesExample5.java */

import java.util.Properties;
import java.util.Enumeration;
import java.io.FileOutputStream;

public class PropertiesExample5
{
	public static void main(String args[])
	{
		try
		{
			//java.util.Properties DECLARATION
			Properties p;

			//java.util.Properties OBJECT CREATION
			p = new Properties();

			p.put("PROMPT", "$p$g");
			p.put("TEMP","C:\\Windows\\Temp");
			p.put("MYAPPNAME", "Huda Tutorials");

			//FileOutputStream CREATION
			FileOutputStream fos = new FileOutputStream("myproperties.xml");

			//SAVE Properties INTO FILE
			p.storeToXML(fos, "Environment Settings");

			//CLOSE THE FileOutputStream
			fos.close();
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e);
		}
	}
}
                        
java.util.Properties class Example 6
/* This example shows how to load Properties from XML File */
/* Save with file name PropertiesExample6.java */

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

public class PropertiesExample6
{
	public static void main(String args[])
	{
		try
		{
			//java.util.Properties DECLARATION
			Properties p;

			//java.util.Properties OBJECT CREATION
			p = new Properties();

			//FileInputStream CREATION
			FileInputStream fis = new FileInputStream("myproperties.xml");

			//LOAD Properties FROM FILE
			p.loadFromXML(fis);

			//CLOSE THE FileInputStream
			fis.close();

			//DISPLAY RETRIEVED Properties
			p.store(System.out," RETRIEVED Properties");
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e);
		}
	}
}