Application attributes are often called preferences and can often allow the user to
configure various start up options, preferred window size, or whatever.
A D V E R T I S E M E N T
Setting Program Attributes
A program typically needs this information about the system and application environment
to make decisions about how to do something, or what to do. Also, a program may wish to
modify certain attributes itself, or allow a user to change them. Thus a program needs
to be able to read and write various system attributes and program-specific attributes.
Java programs can manage these attributes through three mechanisms: properties,
application command line arguments, and applet parameters.
Use properties to define environmental attributes on a persistent basis. That is, use
properties when they need to be in effect for each invocation of a program.
Application Command Line Arguments
Use command line arguments to define or modify environmental attributes on a
non-persistent basis. That is, use command line arguments to change one or more
attributes for just one invocation of a program.
Use applet parameters to define or modify environmental attributes on a non-persistent
basis for applets. That is, use parameters to set one or more attributes for a single
invocation of an applet.
Converting Objects to Strings
You can use the Properties class from java.util to manage attributes specific to your
Java programs. A Properties object manages a set of key/value pairs: the key represents
the name of a property and the value is the current value of the property. You can load
key/value pairs into a Properties object from a stream, save the properties to a stream,
and get information about the properties represented by the Properties object.
Setting up Your Properties Object
Often when a program starts up, it will use code similar to the following to set up the
// set up default properties
Properties defaultProps = new Properties();
FileInputStream defaultStream = new FileInputStream("defaultProperties");
// set up real properties
Properties applicationProps = new Properties(defaultProps);
FileInputStream appStream = new FileInputStream("appProperties");
First the application sets up a default Properties object. This object contains the set
of properties to use if values are not explicitly set elsewhere. This code snippet uses
the load() method to read the default values from a file on disk named defaultProperties.
Applications usually save and restore properties to files on the disk.
Next, the program uses a different constructor to create a second Properties object,
applicationProps. This object uses defaultProps to provide its default values.
Then the code snippet loads a set of properties into applicationProps from a file named
appProperties. The properties loaded into appProperties can be set on a per user basis,
a per site basis, or whatever is appropriate for the current application.
Getting Property Information
Once you've set up your Properties object, you can query it for information about
various properties it contains. The Properties class provides several methods for
getting property information
getProperty() (2 versions)
returns the value for the specified property. One version allows you to provide a
default value--if the key is not found the default is returned.
writes all the properties to the specified stream. This is useful for debugging.
returns an Enumeration containing all of the keys contained in the Properties object.
Command Line Arguments
Your Java application can accept any number of arguments from the command line. Command
line arguments allow the user to affect the operation of an application. For example,
a program might allow the user to specify verbose mode--that is, specify that the
application display a lot of trace information--with the command line argument -verbose.
When invoking an application, the user types the command line arguments after the
application name. For example, suppose you had a Java application, called Sort, that
sorted lines in a file, and that the data you want sorted is in a file named
ListOfFriends. If you were using DOS, you would invoke the Sort application on your
data file like this:
C:\> java Sort ListOfFriends
Echo Command Line Arguments
This simple application displays each of its command line arguments on a line by itself:
public static void main (String args)
for (int i = 0; i < args.length; i++)
Try this: Invoke the Echo application with the command line shown in this DOS example:
C:\> java Echo Drink Hot Java
You'll notice that the application displays each word--Drink, Hot, and Java--on a line
by itself. This is because The Space Character Separates Command Line Arguments.