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 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 is 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 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 a 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

//For properties class we required this package...
import java.util.*;

//For input-output we required this package....
import java.io.*;

class PropertiesReader

  {

    public static void main(String args[])throws Exception

      {

        Properties p=new Properties();

        System.out.println("Loading Properties from a.properties file...");

        p.load(new FileInputStream("b.properties"));

        Set set=p.entrySet();

        Iterator itr=set.iterator();

        System.out.println("Course\t Fees");

        while(itr.hasNext())

          {

            Map.Entry m=(Map.Entry)itr.next();

            System.out.println(m.getKey() + "\t" + m.getValue());

          }

      }

  }

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

//For properties class we required this package...

import java.util.*;

//For input-output we required this package....

import java.io.*;

class PropertiesSaver

  {

    public static void main(String args[])throws Exception

      {

        Properties p=new Properties();

        p.setProperty("Ravi", "Java");

        p.setProperty("Rahul", ".net");

        p.setProperty("Raj", "php");

        p.setProperty("Sandeep", "Java");

        Set set=p.entrySet();

        Iterator itr=set.iterator();

        System.out.println("Following properties are saved in trainer.properties file");

        System.out.println("Trainer\tCourse");

        while(itr.hasNext())

          {

            Map.Entry m=(Map.Entry)itr.next();

            FileOutputStream fout=new FileOutputStream("trainers.properties");

            p.store(fout,"Trainer Information");

            System.out.println("Successfully saved..");

          }

      }

  }

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.

Up Next
    Ebook Download
    View all
    Learn
    View all