"JAVA Properties class overview"

Source: Internet
Author: User

I. Overview.

As I said before, this object is a technology that is combined with IO flow, so it is more appropriate to combine IO streams.

Propertiesextends hashtable<object,object>

The hierarchy of inheritance for this class:

Java.lang.Object

|--java.util.dictionnary<k,v>

|--java.util.hashtable<object,object>

|--java.util.properties

Jdk1.6api describes it:

Propertiesclass represents a persisted set of properties. Propertiescan be saved in a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

A property list can contain another property list as its default value, or a second property list if it fails to search for the property key in the original property list.

In simple terms, the properties class is characterized by:

1. The keys and values in the collection are string types.
2. The data in the collection can be saved to the stream or fetched from the stream.
3. This collection is typically used to manipulate configuration files that exist as key-value pairs.

Second, the commonly used methods.

0. Construction method.

Properties()
Creates a list of empty properties with no default values.
Properties(Properties defaults)
Creates a list of empty properties with the specified default value.

The first method has no default value, and the second method has a parameter that is the properties type, which is actually the default property list for the newly created Properties object in the defaults. However, the newly created object does not change the contents of the original defaults (no matter what additions or deletions), which may cause some strange phenomena.

1 Private Static voidpropertiesDemo01 () {2Properties p=NewProperties ();3P.setproperty ("Zhangsan", "the");4P.setproperty ("The Lisi", "the");5P.setproperty ("Wangwu", "the");6Properties pp=NewProperties (p);7         8Pp.remove ("01");9System.out.println (Pp.getproperty ("01"));TenSystem.out.println (P.getproperty ("01")); One          AP.remove ("01"); -System.out.println (P.getproperty ("01")); -System.out.println (Pp.getproperty ("01")); the}
View Code

The result of this code execution is:

1 Zhangsan 2 Zhangsan 3 NULL 4 NULL
Execution Results

As you can see, the newly created object will still be able to output the specified information even if the defaults key pair is deleted, because its operation does not affect the contents of the defaults. But the query is not the same, if the specified key is not found in the current attribute list, it will go to the default property list and return null if not. We can find here, in fact, defaults is the "behind the rulers", the newly created objects, whether or not to change the contents of the object will not affect its content, but if its own additions and deletions to change some content, it will affect the newly created object.

1. Adding, deleting, modifying, traversing, and fetching values

"1" adding, deleting, modifying, and fetching values

GetProperty (String key)
Searches for a property in this property list with the specified key. If the key is not found in this property list, the default property list and its default values are then recursively checked. if the property is not found, this method null returns .
GetProperty (string key, String defaultvalue)
Searches for a property in the property list with the specified key. If the key is not found in the attribute list, the default property list and its default values are then recursively checked. if the property is not found, this method returns the default value variable .
SetProperty (String key,string value)
Call the Hashtable method put . Use the GetProperty method to provide parallelism. Enforces the use of strings for the keys and values of the properties. The return value is put The result of the Hashtable call.
This method is both a method of adding a key-value pair and a method for modifying a key-value pair.  
Remove (Object key)
removes the key and its corresponding value from the hash table. If the key is not in the hash table, this method does nothing.
Note: This method is inherited from the parent class, and the properties class itself does not provide a way to delete the specified key-value pair.

Example:

1 Private Static voidpropertiesDemo01 () {2Properties p=NewProperties ();3P.setproperty ("Zhangsan", "the");4P.setproperty ("The Lisi", "the");5P.setproperty ("Wangwu", "the");6         7P.remove ("01");8 p.list (System.out);9System.out.println (P.getproperty ("04"));TenSystem.out.println (P.getproperty ("04", "Zhao Liu")); One}
View Code

"2" traversal

PropertyNames ()
Returns an enumeration of all the keys in a property list, including a different key in the default attribute list if a key with the same name is not found in the main attribute list.
Return:
An enumeration of all the keys in the property list, including the keys in the default Properties list.
Stringpropertynames ()
Returns the set of keys in this property list, where the key and its corresponding value are strings, and if a key with the same name is not found in the main attribute list, the different keys in the default attribute list are also included. Properties whose key or value is not a String type are ignored.

The returned set is not supported by the Properties object. Changes to this Properties cannot be reflected in the set, and vice versa.

Return:
The set of keys in this property list, where the key and its corresponding value are strings, including the keys in the default Properties list.
Start from the following versions:
1.6

Of the above two methods, the first method is a less commonly used method because the return value of the method is of the enumerated type, and the enumeration type is deprecated now. So the most commonly used method is the second method.

"JAVA Properties class overview"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.