Properties Java Help

Properties

Properties is a subclass of Hash table, It is used to maintain lists of values in which the key is a String and the value is also a String. TI,e Properties class is used by many , ‘ other Java classes. For example, it is the type of object returned by System get Properties( ) when obtaining environmental values. Properties defines the following instance variable: Properties defaults; This variable holds a default property list associated with it Properties object.Properties defines these constructors: Properties( ) Properties(Properties propeller first version creates a Properties object that has no default values. The second creates an object that uses prop Default for its default values. In both cases, the property list is empty.In addition to the methods that Properties inherits from Hash table, Properties defines the methods listed Properties also contains one deprecated method: save( ). This was replaced by store( because save( }did not handle. errors correctly.

One useful capability of the Properties class is that you can specify a default property that will be’ returned if no value is associated with a certain key. For example, a default value can be specified along with the key in the get Property() method-such as get Property(“name”, “default value”). If the “name” value is not found hen “default value” is muted, When you construct a Properties object, you can P;)SS another instance of-Properties to be used as the default properties for the new instance. In this case, if you call get Property”foo”) on a given Properties object, and “foo” docs not exist, Java looks for “foo” in the default Properties object. This allows for arbitrary nesting of levels of default properties.

The following example demonstrates Properties. It creates a property list in which the keys are the names of states and the values arc the names. of their capitals. Notice that the attempt to find the capital for Florida includes a default value. The output from this program is shown here:

The capital of California
The capital of Washington
The capital of Missouri
The capital of Indignant
The capital of Illinois
The capital of Florida is Not

Since Florida is not in the list, the default value is used.

Although it is perfectly valid to use a default value when you call get Property ( I, as .the preceding example shows, there is a better way of handling default values for most applications of property lists. For greater flexibility, specify a default property list when constructing a Properties object. The default list will be searched if the desired key is not found in the main list. For example, the following is a slightly reworked version of the preceding program, with a default list of states specified. Now, when Florida is sought, it will be found in the default list:

JavaHelpOnline.com

  • Feel free to send us an inquiry, we will reply back in hours.
 

Verification

Posted on September 17, 2014 in java util Part 1 The Collections Framework

Share the Story

About the Author

Back to Top