Publish Your Article Here

Program: How to get all keys from properties file?


This example shows how to get all keys from the given properties file. All keys will be returned in the form of set object. You can get it by calling keySet() method.


import java.util.Properties;
import java.util.Set;

public class MyPropAllKeys {

        private Properties prop = null;
        public MyPropAllKeys(){
                InputStream is = null;
                try {
                        this.prop = new Properties();
                        is = this.getClass().getResourceAsStream("/");
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
        public Set<Object> getAllKeys(){
                Set<Object> keys = prop.keySet();
                return keys;
        public String getPropertyValue(String key){
                return this.prop.getProperty(key);
        public static void main(String a[]){
                MyPropAllKeys mpc = new MyPropAllKeys();
                Set<Object> keys = mpc.getAllKeys();
                for(Object k:keys){
                        String key = (String)k;
                        System.out.println(key+": "+mpc.getPropertyValue(key));

db.password: mypassword
db.user: java2novice
db.service: orcl
<< Previous Program | Next Program >>

List of Properties class sample programs:

  1. How to load Properties file from a file system?
  2. How to load Properties file from the classpath?
  3. How to load Properties file from a static block or static method?
  4. How to assign default values for unavailable keys in properties file?
  5. How to get all keys from properties file?
  6. How to create and store property file dynamically?
  7. How to store property file as xml file?
  8. How to load property file using class name in java?
Knowledge Centre
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.