Miscellaneous Platform Features

The following sections detail important but miscellaneous features of the Java platform, including properties, preferences, processes, and management and instrumentation.

Properties

java.util.Properties is a subclass of java.util.Hashtable, a legacy collections class that predates the Collections API introduced in Java 1.2. A Properties object maintains a mapping between string keys and string values and defines methods that allow the mappings to be written to and read from a simple text file or (in Java 5.0) an XML file. This makes the Properties class ideal for configuration and user preference files. The Properties class is also used for the system properties returned by System.getProperty( ):

import java.util.*; import java.io.*; // Note: many of these system properties calls throw a security exception if // called from untrusted code such as applets. String homedir = System.getProperty("user.home"); // Get a system property Properties sysprops = System.getProperties(); // Get all system properties // Print the names of all defined system properties for(Enumeration e = sysprops.propertyNames(); e.hasMoreElements();) System.out.println(e.nextElement()); sysprops.list(System.out); // Here's an even easier way to list the properties // Read properties from a configuration file Properties options = new Properties(); // Empty properties list File configfile = new File(homedir, ".config"); // The configuration file try { options.load(new FileInputStream(configfile)); ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.