The perfect place for easy learning...

Java Programming

×

Topics List


Properties class in java





In java, the package java.util contains a class called Properties which is a child class of Hashtable class. It implements interfaces like Map, Cloneable, and Serializable.

Java has this built-in class Properties which allow us to save and load multiple values from a file. This makes the class extremely useful for accessing data related to configuration.

The Properties class used to store configuration values managed as key, value pairs. In each pair, both key and value are String values. We can use the key to retrieve the value back when needed.

The Properties class provides methods to get data from the properties file and store data into the properties file. It can also be used to get the properties of a system.

🔔 The Properties class is child class of Hashtable class.

🔔 The Properties class implements Map, Cloneable, and Serializable interfaces.

🔔 The Properties class used to store configuration values.

🔔 The Properties class stores the data as key, value pairs.

🔔 In Properties class both key and value are String data type.

🔔 Using Properties class, we can load key, value pairs into a Properties object from a stream.

🔔 Using Properties class, we can save the Properties object to a stream.

The Properties class in java has the following constructors.

S. No. Constructor with Description
1 Properties( )

It creates an empty property list with no default values.

2 Properties(Properties defaults)

It creates an empty property list with the specified defaults.

The Properties class in java has the following methods.

S.No. Methods with Description
1 void load(Reader r)

It loads data from the Reader object.

2 void load(InputStream is)

It loads data from the InputStream object.

3 void store(Writer w, String comment)

It writes the properties in the writer object.

4 void store(OutputStream os, String comment)

It writes the properties in the OutputStream object.

5 String getProperty(String key)

It returns value associated with the specified key.

6 String getProperty(String key, String defaultValue)

It returns the value associated with given key; or defaultValue if the Properties contains no mapping for the key.

7 void setProperty(String key, String value)

It calls the put method of Hashtable.

8 Enumeration propertyNames())

It returns an enumeration of all the keys from the property list.

9 Set stringPropertyNames()

Returns a set view of the keys of the Properties.

10 void list(PrintStream out)

It is used to print the property list out to the specified output stream.

11 void loadFromXML(InputStream in)

It is used to load all of the properties represented by the XML document on the specified input stream into this properties table.

12 void storeToXML(OutputStream os, String comment)

It writes the properties in the writer object for generating XML document.

13 void storeToXML(Writer w, String comment, String encoding)

It writes the properties in the writer object for generating XML document with the specified encoding.

Let's consider an example program to illustrate methods of Properties class to store a user configuration details to a properties file.

Example
import java.io.*;
import java.util.*;

public class PropertiesClassExample {

	public static void main(String[] args) {

		FileOutputStream fos = null;
		File configFile = null;
		
		try {
			configFile = new File("config.properties");
			fos = new FileOutputStream(configFile);
			Properties configProperties = new Properties();
			
			configProperties.setProperty("userName", "btechsmartclass");
			configProperties.setProperty("password", "java");
			configProperties.setProperty("email", "user@btechsmartclass.com");
			
			configProperties.store(fos, "Login Details");
			
			fos.close();
			
			System.out.println("Configuration saved!!!");
		}
		catch(Exception e) {
			System.out.println("Something went wrong while opening file");
		}	
	}
}

When we execute the above code, it produce the following output.

Properties class in java

Let's consider another example program to illustrate methods of Properties class using console.

Example
import java.util.*;

public class PropertiesClassExample {

	public static void main(String[] args) {

		Properties sites = new Properties(); 
		String str;
		
		sites.put("search", "www.google.com");
		sites.put("learn", "www.btechsmartclass.com");
		sites.put("mail", "www.gmail.com");
		sites.put("social", "www.facebook.com");
		sites.put("watch", "www.youtube.com");

		System.out.println("Keys => " + sites.keySet() + "\n");
		
		Iterator itr = sites.keySet().iterator(); 
		while(itr.hasNext()) 
        { 
            str = (String)itr.next(); 
            System.out.println("The link for " + str + " is " + sites.getProperty(str)); 
        } 
		
		System.out.println("\nThe link for learn is " + sites.getProperty("learn"));
		System.out.println("\nThe link for learning is " + sites.getProperty("learning", "not found"));
	}

}

When we execute the above code, it produce the following output.

Properties class in java