Get All Keys and Values of the Properties files in Java

In this section, you will learn how to get all keys and values of the properties files in the Java.

Get All Keys and Values of the Properties files in Java

In this section, you will learn how to get all keys and values of the properties files in the Java.

Get All Keys and Values of the Properties files in Java

Get All Keys and Values of the Properties files in Java

     

In this section, you will learn how to get all keys and values of the properties files in the Java. This section illustrates the way for getting all keys and corresponding values of the properties file through an example. The example is given bellow.

Description of program:

Here, you will get all keys and values of the properties files with the help of this program. The program shows the keys and values when you run it. 

The following methods are explained as follows which have been used in the program.

pro.keySet():
This method returns a set of keys, which contains in the Properties method.

pro.keys():
This method provides the all keys of the properties files stored in the Enumeration.

Enumeraton:
It's the interface provided by the java.util package. Enumeration implements a series of elements and calls the nextElements method, which provides a series of elements. The keys and values of the properties files that are enumerated in this interface calls by hashtable, which defines the keys and values of the properties files.

hasMoreElements():
This method checks the Enumeration that has one more elements or not with returns true or false. If it returns true, then the Enumeration object has more than one element, otherwise it return false.

nextElement():
This is the method to return the next elements of the Enumeration. If the enumeration provides the more than one elements,

Here is the code of program:

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

public class GetAllProperties{
  public static void main(String[] args) {
  Properties pro = new Properties();
  try{
  FileInputStream in = new FileInputStream("Message.properties");
  pro.load(in);
  System.out.println("All keys of the property file : ");
  System.out.println(pro.keySet());
  System.out.println("All values of the property file : ");
  Enumeration em = pro.keys();
  while (em.hasMoreElements()){
  String str = (String)em.nextElement();
  System.out.println(pro.get(str));
  }
  }
  catch (IOException e){
  System.out.println(e.getMessage());
  }
  }
}

Download this example.