tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Properties > Access Properties

Access Properties 

Properties is simplified version of a Map implementation where the key and value are String objects. Properties extends Hashtable implementation to exhibit Map behavior. The following example shows instantiating and accessing Properties,

File Name  :  
com/bethecoder/tutorials/utils/props/IterateProps.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.utils.props;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

public class IterateProps {
  /**
   @param args
   */
  public static void main(String[] args) {

    Properties prop = new Properties();
    prop.setProperty("ONE""111111");
    prop.setProperty("TWO""222222");
    prop.setProperty("THREE""333333");
    prop.setProperty("FOUR""444444");
    
    System.out.println(prop.getProperty("ONE"));
    System.out.println(prop.getProperty("TWO"));
    System.out.println("------------------------");
    
    Enumeration<?> enu = prop.propertyNames();
    String key;
    String value;
    
    while (enu.hasMoreElements()) {
      key = enu.nextElement().toString();
      value = prop.getProperty(key);
      
      System.out.println(key + "==" + value);
    }

    System.out.println("------------------------");
    Iterator<String> it = prop.stringPropertyNames().iterator();
    
    while (it.hasNext()) {
      key = it.next();
      value = prop.getProperty(key);
      
      System.out.println(key + "==" + value);
    }
  }
}
   

It gives the following output,
111111
222222
------------------------
TWO==222222
ONE==111111
FOUR==444444
THREE==333333
------------------------
TWO==222222
ONE==111111
FOUR==444444
THREE==333333


                    Print

   



Books Recommendation

 
 
bl  br