Java.util.PropertyResourceBundle.getKeys() Method


Advertisements


Description

The java.util.PropertyResourceBundle.getKeys() method returns keys of this PropertyResourceBundle and its parent as an Enumeration.

Declaration

Following is the declaration for java.util.PropertyResourceBundle.getKeys() method

public Enumeration getKeys()

Parameters

  • NA

Return Value

This method returns keys of this and its parent bundles.

Exception

  • NA

Example

The following example shows the usage of java.util.PropertyResourceBundle.getKeys() method.

package com.tutorialspoint;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.Enumeration;
import java.util.PropertyResourceBundle;

public class PropertyResourceBundleDemo {
   public static void main(String[] args) {
   // Prepare content for simulating property files
   String fileContent = 
   "# Integer value 1\n" +
   "s1=1\n" +
   "# String value PropertyResourceBundleDemo\n" +
   "s2=PropertyResourceBundleDemo\n" +
   "# Date value Fri Jan 31 00:00:00 IST 3913\n" +
   "s3=Fri Jan 31 00:00:00 IST 3913";
   InputStream propStream = new StringBufferInputStream(fileContent);
      
   try {
   // Create property resource bundle
   PropertyResourceBundle bundle = 
   new PropertyResourceBundle(propStream);
         
   // Get resource keys
   Enumeration keys = bundle.getKeys();
   while(keys.hasMoreElements()) {
   System.out.println("Bundle key: "+keys.nextElement());
   }
   } catch (IOException e) {
   e.printStackTrace();
   }
      
   }

}

Let us compile and run the above program, this will produce the following result:

Bundle key: s2
Bundle key: s1
Bundle key: s3

java_util_propertyresourcebundle.htm

Advertisements