How the Properties Class in Java Works

Introduction

 
This article explains how the properties class of Java works with two examples. This article explains the properties class, properties files, use of properties files, some commonly used methods of the properties class and provides two examples to help the understanding of how the properties class works. In the first example, we display a properties file using the program and in the second example, we create a properties file by the program.
 

What the properties class is?

 
The properties class is a class for creating and using properties files in Java applications. The properties class exists under the "java.util.properties" package, it is a sub-package of the Hashtable class. Hashtable is a thread-safe version of HashMap.
 

What a properties file is?

 
A properties file is a text file that contains variable textual information in the form of a key/value pair. In simple words, it is a text file for storing information like in a database.
 

What's the use of a properties file?

 
Let's talk in the developer mode. When we create a small application (like developing college websites, educational websites, coaching institute websites, etcetera) we also need a database for that. If we implement a separate database for those websites then it will increase the burden of the developer to maintain those databases. So instead of a database, Sun Microsystems provides a properties file to handle such small Java applications.
 
The main advantages of this type of file are that when we need some changes in the file then we simply change the data, we don't need to change the programming code for it.
The following figure shows how the properties class works.
 
Fig-1.jpg
1.0: A properties object is created.
1.1: Properties object is asked to load data from the properties file.
1.2: Properties are loaded from the file and are stored in a HashTable. 
1.3: Properties value are fetched.
 
Some commonly used public method of the properties class are:
  • void setProperty(String Key, String Value)
          Used to store a property.
  • string getProperty(String Key);
          Used to obtain the value of a property.
  • void load(FileInputStream finp)
          Used to load property from a property file.
  • void store(FileOutputStream foutp, String IdentifierComment)
          Used to store properties to a property file.
 
Example 1:
 
In this example, we display a properties file using the following program. For displaying a properties file we need a properties file. So first we create a properties file as in the following.
 
Open a text editor (like Notepad, WordPad, Microsoft Word, etcetera) and write anything you want to be displayed. I wrote the following.
 
fig-2.jpg
 
Now save your file with a .properties extension (I saved mine using "a.properties") in the folder where you saved your Java program.
 
Now type the following code in your program.
 
PropertiesReader.java
  1. import java.util.*;  
  2. import java.io.*;  
  3. class PropertiesReader   
  4. {  
  5.  public static void main(String args[]) throws Exception   
  6.  {  
  7.   Properties p = new Properties();  
  8.   System.out.println("Loading Properties from a.properties file...");  
  9.   p.load(new FileInputStream("b.properties"));  
  10.   Set set = p.entrySet();  
  11.   Iterator itr = set.iterator();  
  12.   System.out.println("Course\t Fees");  
  13.   while (itr.hasNext())   
  14.   {  
  15.    Map.Entry m = (Map.Entry) itr.next();  
  16.    System.out.println(m.getKey() + "\t" + m.getValue());  
  17.   }  
  18.  }  
  19. }   
Output
 
fig-3.jpg
 
After pressing Enter we get the following output.
 
In this output, we saw how we can display the contents of the properties file.
 
fig-4.jpg
 
Now see the advantage
 
Now we make some changes in our file (b.properties). Add some values there as shown in the following figure.
 
fig-5.jpg
 
Now run our program again and see the benefits of this file.
 
fig-6.jpg
 
 
Note: Now you see that without changing the program code we have made changes in the file and displayed it easily. So that's why a properties file is used in small applications instead of databases, this file is easy to handle, developed and maintenance is also good.
 
Example 2
 
In this example, we create a properties file using the following program. For creating a properties file we require the FileOutputStream method.
 
PropertiesSaver.java
  1. import java.util.*;  
  2. import java.io.*;  
  3. class PropertiesSaver   
  4. {  
  5.  public static void main(String args[]) throws Exception   
  6.  {  
  7.   Properties p = new Properties();  
  8.   p.setProperty("Ravi""Java");  
  9.   p.setProperty("Rahul"".net");  
  10.   p.setProperty("Raj""php");  
  11.   p.setProperty("Sandeep""Java");  
  12.   Set set = p.entrySet();  
  13.   Iterator itr = set.iterator();  
  14.   System.out.println("Following properties are saved in trainer.properties file");  
  15.   System.out.println("Trainer\tCourse");  
  16.   while (itr.hasNext())   
  17.   {  
  18.    Map.Entry m = (Map.Entry) itr.next();  
  19.    FileOutputStream fout = new FileOutputStream("trainers.properties");  
  20.    p.store(fout, "Trainer Information");  
  21.    System.out.println("Successfully saved..");  
  22.   }  
  23.  }  
  24. }   
Output
 
Fig-7.jpg
 
Now open the file ("trainer.properties") to see the data in the file since the following data will be stored there. First, enter the following command:
 
fig-8.jpg
 
Press Enter to see the content of the file. Note that a new field of date and time is created there. Don't worry about that, it is generated by the compiler automatically.
 
fig-9.jpg
 
Thanks For Reading.