Ask Questions?

View Latest Questions

Advertisement


 
 

Properties
Posted on: July 26, 2006 at 12:00 AM
The function of the java.util.Properties class.

Java Notes

Properties

Note: The function of the java.util.Properties class, to save and restore the properties for a program, might be easier to do using java.util.Preferences. See Preferences.

The java.util.Properties class stores and loads key/value pairs from a file, and manages them in memory. This is the class to use to implement persistent variables (values which are saved in a file).

To create and put values in a Properties table

This example creates a new Properties table and assigns a string value to four different keys.

Properties props = new Properties(); 
props.setProperty("recursiveSearch", "true");
props.setProperty("noCopyPattern", "*.$$$");
props.setProperty("maxLevel", "7");
props.setProperty("fileName", "C:\temp\work.html");

To store a Properties table in a file

Use the Properties .store(OutputStream, String) method. It will write the properties to the output stream, with a header line from the String parameter. Assuming you have variables and an OutputStream like:

boolean recursiveSearch;
String noCopyPattern;
int maxLevel;
. . .
try {
  OutputStream propOut = new FileOutputStream(
            new File("props.stat"));
}catch (. . .

To change values from their natural types to String, the usual idiom is to use concatenation of the empty string..

props.setProperty("recursiveSearch", ""+recursiveSearch);
props.setProperty("noCopyPattern"  , noCopyPattern");
props.setProperty("maxLevel"       , ""+maxLevel);

props.store(propOut, "Macro Processor Properties");

To load Properties from a file

Use the Properties .load(InputStream); method.

You will probably want to get each property value and assign it to an internal variable and perhaps show it in the user interface. For example,

boolean recursiveSearch;
String noCopyPattern;
int maxLevel;
. . .
props.load(propsIn);

// Change the properties back into their natural types.
recursiveSearch = Boolean.getBoolean(props.getProperty("recursiveSearch"));
noCopyPattern = props.getProperty("noCopyPattern");
maxLevel = Integer.parseInt(props.getProperty("maxLevel");

To use default properties

  1. Create a Properties table with all of the default key/value pairs. For example,
    Properties defaultProps = new Properties();
    defaultProps.setProperty("sourceFile", "");
    defaultProps.serProperty("enableScrolling", "false");
  2. Use this default table in the constructor for your regular properties table:
    Properties props = new Properties(defaultProps);

Constructor and Method Summary

Properties()
Properties(Properties defaultValues)

Object setProperty(String key, String value)
String getProperty(String key)
String getProperty(String key, String defaultValue)
Enumeration propertyNames()

void   load(InputStream in)
void   store(OutputStream out, String header)

Related Pages

Copyleft 2004 Fred Swartz MIT License
Advertisement


DMCA.com