HudaTutorials.com

Java Properties Class - Properties Class in Java

Last updated on

java.util.Properties Class

Properties Class in Java

Java Properties Class

The Java 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.

The Properties class does not inherit the concept of a load factor from its superclass, Hashtable.

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

What is Java Properties Class ?

The java.util.Properties class is a class which represents a persistent set of properties.The Properties can be saved to a stream.

java.util.Properties class Example

/*	Java 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);
		}
	}
}

In the following Java Properties class example, you can learn how to get system Properties.

How to get System Properties using Java Properties class

/*	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;
	}
}

How to save Java Properties into File

In the following Java Properties class example, you can learn how to save Java Properties into file.

/*	This example shows how to save Java 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);
		}
	}
}

In the following Java Properties class example, you can learn how to load Java Properties from file.

How to load Java Properties from File

/*	This example shows how to load Java 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);
		}
	}
}

In the following Java Properties class example, you can learn how to save Java Properties into XML file.

How to save Java Properties into XML File

/*	This example shows how to save Java 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);
		}
	}
}

In the following Java Properties class example, you can learn how to load Java Properties from XML file.

How to load Java Properties from XML File

/*	This example shows how to load Java 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);
		}
	}
}