Home Java Example Java Util Get All Keys and Values of the Properties files in Java

Related Tutorials


 
 

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 it's 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 it's values of the properties files in Java. Java provides the features to collection of all keys and values of the properties file. The properties files that can be used to large collections of information to be inserting in the properties files.

Here, you will get the all keys and values of the properties files with the help of this program. The program shows the keys and it's values when you run it. The following methods had been used by the program.

pro.keySet():
This method returns a set of keys, which contained in the object of the Properties method. The Properties files has the many keys then this method provides all keys.

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

Enumeraton:
This is the interface which provided by the java.util package. It's object implements the series of elements and calls the nextElements method, which provides the series of elements. The keys and values of the Properties files which is enumerated by this interface and calls the hashtable which defines the keys and values of the properties files.

hasMoreElements():
This method checks the Enumeration object has more elements or not and returns the true or false. It returns true, if the Enumeration object has more than one elements Otherwise, it return false.

nextElement():
This is the method to returns 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{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter file name for getting all properties : ");
  FileInputStream in = new FileInputStream(br.readLine() ".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(str + ": " + pro.get(str));
  }
  }
  catch(IOException e){
  System.out.println(e.getMessage());
  }
  }
}

Download this example

 

Advertisement

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 16, 2007

Related Tutorials

Discuss: Get All Keys and Values of the Properties files in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Ducky
January 16, 2012
njasNEKfcDvCAEBVd

Thanky Thanky for all this good informtoain!
DMCA.com