Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

 

                         

» View all related tutorials
Related Tags: c string reference io method type boolean key rmi ole this ai node oo create if boo example to contains

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

I read your example code here. Thanks for this.

What if we have duplicate values in the property file, e.g.

name = rahul
name = jeet
age = 10
code = 30

here the name is duplicate, if i use your sample code I only can get the following:

name = jeet
age = 10
code = 30

Which does not give all the property values. Could you please suggest a way so i can retrieve the duplicate fields from the property file as well. My property file contain lots of duplicate elements and i want to read all.

Thanks in advance for your help.

BR,
Rahul

Posted by Rahul on Tuesday, 08.5.08 @ 13:05pm | #71066

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.