Home Java Example Java Util Properties Get All Keys and Values of the Properties files in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Get All Keys and Values of the Properties files in Java

Advertisement
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. 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.

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 3, 2006

Related Tutorials

Ask Questions?    Discuss: Get All Keys and Values of the Properties files in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com