Java Properties class

Introduction

Java Properties class is a subclass of Hashtable.

It keeps a list of value pairs in which the key is a String and the value is also a String.

It is the type of object returned by System.getProperties().

// Demonstrate a Property list.  
import java.util.*;  

public class Main {  
  public static void main(String args[]) {  
    Properties capitals = new Properties();  
  
    capitals.put("Illinois", "Springfield");  
    capitals.put("Missouri", "Jefferson City");  
    capitals.put("Washington", "Olympia");  
    capitals.put("California", "Sacramento");  
    capitals.put("Indiana", "Indianapolis");  
  
    // Get a set-view of the keys. 
    Set<?> states = capitals.keySet(); 
  
    // Show all of the states and capitals. 
    for(Object name : states)  
      System.out.println("The capital of " +  
                         name + " is " +  
                         capitals.getProperty((String)name)  
                         + ".");  
  
    System.out.println();  /*w ww. j  a  va2 s  . c o  m*/
  
    // Look for state not in list -- specify default. 
    String str = capitals.getProperty("Florida", "Not Found");  
    System.out.println("The capital of Florida is "  
                       + str + ".");  
  }  
}

The following example demonstrates Properties.

// Demonstrate a Property list.  
import java.util.Properties;
import java.util.Set;

public class Main {
  public static void main(String args[]) {
    Properties pro = new Properties();

    pro.put("A", "AA");
    pro.put("B", "BB");
    pro.put("C", "CC");
    pro.put("D", "DD");
    pro.put("E", "EE");

    // Get a set-view of the keys.
    Set<?> states = pro.keySet();

    for (Object name : states)
      System.out.println(name + " is " + pro.getProperty((String) name));

    // Look for value not in list -- specify default.
    String str = pro.getProperty("FF", "Not Found");
    System.out.println(str);/*w w w.  j a v a2  s.c o m*/
  }
}

A Properties object is a Hashtable that stores key-value pairs of Strings.

The Properties object can be written to a file and read back.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    Properties table = new Properties();

    // set properties
    table.setProperty("Key", "value");

    listProperties(table);//from  w  ww  .ja  va 2 s  .c  om

    // replace property value
    table.setProperty("another key", "another value");
    table.setProperty("Key", "new value");

    listProperties(table);

    saveProperties(table);

    table.clear(); // empty table

    listProperties(table);

    loadProperties(table);

    // get value of property color
    Object value = table.getProperty("Key");

    // check if value is in table
    if (value != null)
      System.out.println("found Value:"+ value);
    else
      System.out.println("not found");
  }
  private static void saveProperties(Properties props) {
    try {
      FileOutputStream output = new FileOutputStream("props.dat");
      props.store(output, "Sample Properties"); // save properties
      output.close();
      System.out.println("After saving properties");
      listProperties(props);
    } catch (IOException ioException) {
      ioException.printStackTrace();
    }
  }
  private static void loadProperties(Properties props) {
    // load contents of table
    try {
      FileInputStream input = new FileInputStream("props.dat");
      props.load(input); // load properties
      input.close();
      System.out.println("After loading properties");
      listProperties(props);
    } catch (IOException ioException) {
      ioException.printStackTrace();
    }
  }

  // output property values
  private static void listProperties(Properties props) {
    Set<Object> keys = props.keySet(); // get property names

    // output name/value pairs
    for (Object key : keys)
      System.out.printf("%s\t%s%n", key, props.getProperty((String) key));

    System.out.println();
  }
}



PreviousNext

Related