«Back to Home

Core Java

Topics

Properties Class In Java

Properties Class
 
In Java, the properties object holds both key and value pair as a string. It is the sub class of Hashtable and we can use to get the property value, based on the property key.
 
Properties class provides the methods to get the data from the properties file and store the data into the properties file. Besides, it can be used to get the properties of the system.
 
Advantage of Properties File

Maintenance is very easy- When any information is changed from the properties file, we don't need to recompile the class of Java. It is generally used to hold the variable information i.e. to be changed.
 
Constructors of Properties Class
 
Properties( )

This constructor is used to create properties object, which has no default values.
 
Properties(Properties propDefault)

This constructor is used to create an object, which uses propDefault for its default values. In both cases, the property list is empty.
 
Methods of Properties Class

String getProperty(String key)

This method is used to return the value associated with the key. A null object is returned, if the key is neither in the list nor in the default property list.
 
String getProperty(String key, String defaultProperty)

This method is used to return the value associated with the key. defaultProperty is returned, when the key is neither in the list nor in the default property list.
 
void list(PrintStream streamOut)

This method is used to send the property list to the output stream linked to streamOut.
 
void list(PrintWriter streamOut)

This method is used to send the property list to the output stream linked to streamOut.
 
void load(InputStream streamIn) throws IOException

This method is used to input a property list from the input stream linked to streamIn.
 
Enumeration propertyNames( )

This method is used to return an enumeration of the keys. It includes those keys, which are found in the default property list, too.
 
Object setProperty(String key, String value)

This method is used to associate value with the key. It returns the previous value associated with key, or returns null, if no such association exists.
 
void store(OutputStream streamOut, String description)

This method is used after writing the string, specified by the description, the property list is written to the output stream linked to streamOut.
 
Let’s see an example, given below.
 
Code
  1. import java.util.*;  
  2. public class PropertiesClass {  
  3.     public static void main(String args[]) {  
  4.         Properties c = new Properties();  
  5.         Set s;  
  6.         String st;  
  7.         c.put("Washington""Olympia");  
  8.         c.put("California""Sacramento");  
  9.         c.put("Indiana""Indianapolis");  
  10.         s = c.keySet();  
  11.         Iterator i = s.iterator();  
  12.         while (i.hasNext()) {  
  13.             st = (String) i.next();  
  14.             System.out.println("The capital of " + st + " is " + c.getProperty(st) + ".");  
  15.         }  
  16.         System.out.println();  
  17.         st = c.getProperty("Florida""Not Found");  
  18.         System.out.println("The capital of Florida is " + st + ".");  
  19.     }  
  20. }  
13

Output

14

Summary

Thus, we learnt that the properties object holds both key and value pair as a string. It is the sub class of Hashtable and we can use it to get the property value, based on the property key and we also learnt how to create it in Java.