Properties class represents a persistent set of
properties. The Properties can be saved to 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 "defaults"; this second property list is searched if the property key is not found in the original property list.
Because Properties inherits from Hashtable, the
put and putAll methods can be applied to a
Properties object. Their use is strongly discouraged as they
allow the caller to insert entries whose keys or values are not
Strings. The setProperty method should be used
instead. If the store, save, or list method is called
on a "compromised" Properties object that contains a
non-String key or value, the call will fail. Similarly,
the call to the propertyNames method
will fail if it is called on a "compromised" Properties
object that contains a non-String key.
The iterators returned by the iterator method of this class's
"collection views" (that is, entrySet(), keySet(), and
values()) may not fail-fast (unlike the Hashtable implementation).
These iterators are guaranteed to traverse elements as they existed upon
construction exactly once, and may (but are not guaranteed to) reflect any
modifications subsequent to construction.
The load(Reader) /
store(Writer, String)
methods load and store properties from and to a character based stream
in a simple line-oriented format specified below.
The load(InputStream) /
store(OutputStream, String)
methods work the same way as the load(Reader)/store(Writer, String) pair, except
the input/output stream is encoded in ISO 8859-1 character encoding.
Characters that cannot be directly represented in this encoding can be written using
Unicode escapes as defined in section 3.3 of
The Java Language Specification;
only a single 'u' character is allowed in an escape
sequence.
The loadFromXML(InputStream) and storeToXML(OutputStream, String, String) methods load and store properties
in a simple XML format. By default the UTF-8 character encoding is used,
however a specific encoding may be specified if required. Implementations
are required to support UTF-8 and UTF-16 and may support other encodings.
An XML properties document has the following DOCTYPE declaration:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">Note that the system URI (http://java.sun.com/dtd/properties.dtd) is not accessed when exporting or importing properties; it merely serves as a string to uniquely identify the DTD, which is:
<?xml version="1.0" encoding="UTF-8"?> <!-- DTD for properties --> <!ELEMENT properties ( comment?, entry* ) > <!ATTLIST properties version CDATA #FIXED "1.0"> <!ELEMENT comment (#PCDATA) > <!ELEMENT entry (#PCDATA) > <!ATTLIST entry key CDATA #REQUIRED>
This class is thread-safe: multiple threads can share a single
Properties object without the need for external synchronization.
API Note
The Properties class does not inherit the concept of a load factor
from its superclass, Hashtable.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| protected volatile Properties | defaults
A property list that contains default values for any keys not found in this property list. |
| private transient volatile ConcurrentHashMap | map
Properties does not store values in its inherited Hashtable, but instead in an internal ConcurrentHashMap. |
| private static final long | serialVersionUID
use serialVersionUID from JDK 1.1.X for interoperability
|
| private static final Unsafe |
| Access | Constructor and Description |
|---|---|
| public | |
| public | Properties(int
the initialCapacity)Properties will be sized to
accommodate this many elementsCreates an empty property list with no default values, and with an initial size accommodating the specified number of elements without the need to dynamically resize. |
| public | Properties(Properties
the defaults. defaults)Creates an empty property list with the specified defaults. |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized void | clear()
Overrides java. Implements java. Clears this hashtable so that it contains no keys. |
| public synchronized Object | |
| public synchronized Object | compute(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction)
Overrides java. Overrides default java. Attempts to compute a mapping for the specified key and its current
mapped value, or |
| public synchronized Object | computeIfAbsent(Object key, Function<? super Object, ?> mappingFunction)
Overrides java. Overrides default java. If the specified key is not already associated with a value (or is mapped
to |
| public synchronized Object | computeIfPresent(Object key, BiFunction<? super Object, ? super Object, ?> remappingFunction)
Overrides java. Overrides default java. If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value (optional operation). |
| public boolean | contains(Object value)
Overrides java. Tests if some key maps into the specified value in this hashtable. |
| public boolean | containsKey(Object key)
Overrides java. Implements java. Tests if the specified object is a key in this hashtable. |
| public boolean | containsValue(Object value)
Overrides java. Implements java. Returns true if this hashtable maps one or more keys to this value. |
| public Enumeration | elements()
Overrides java. Returns an enumeration of the values in this hashtable. |
| public Set | entrySet()
Overrides java. Implements java. Returns a |
| private void | |
| private void | enumerateStringProperties(Map<String, String>
the Map h)Enumerates all key/value pairs into the specified Map and omits the property if the key or value is not a string. |
| public synchronized boolean | equals(Object o)
Overrides java. Implements java. Compares the specified Object with this Map for equality, as per the definition in the Map interface. |
| public synchronized void | forEach(BiConsumer<? super Object, ? super Object> action)
Overrides java. Overrides default java. Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. |
| public Object | get(Object key)
Overrides java. Implements java. Returns the value to which the specified key is mapped,
or |
| public Object | getOrDefault(Object key, Object defaultValue)
Overrides java. Overrides default java. Returns the value to which the specified key is mapped, or
|
| public String | Returns: the value in this property list with the specified key value.the property key. key)Searches for the property with the specified key in this property list. |
| public String | Returns: the value in this property list with the specified key value.the hashtable key. key, String a default value. defaultValue)Searches for the property with the specified key in this property list. |
| public synchronized int | hashCode()
Overrides java. Implements java. Returns the hash code value for this Map as per the definition in the Map interface. |
| public boolean | isEmpty()
Overrides java. Implements java. Tests if this hashtable maps no keys to values. |
| public Enumeration | |
| public Set | keySet()
Overrides java. Implements java. Returns a |
| public void | |
| public void | |
| public synchronized void | |
| public synchronized void | load(InputStream
the input stream. inStream)Reads a property list (key and element pairs) from the input byte stream. |
| private void | |
| private String | |
| public synchronized void | loadFromXML(InputStream
the input stream from which to read the XML document. in)Loads all of the properties represented by the XML document on the specified input stream into this properties table. |
| public synchronized Object | merge(Object key, Object value, BiFunction<? super Object, ? super Object, ?> remappingFunction)
Overrides java. Overrides default java. If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value (optional operation). |
| public Enumeration | Returns: an enumeration of all the keys in this property list, including the keys in the default property list.Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list. |
| public synchronized Object | put(Object key, Object value)
Overrides java. Implements java. Maps the specified |
| public synchronized void | putAll(Map<?, ?> t)
Overrides java. Implements java. Copies all of the mappings from the specified map to this hashtable. |
| public synchronized Object | putIfAbsent(Object key, Object value)
Overrides java. Overrides default java. If the specified key is not already associated with a value (or is mapped
to |
| pack-priv void | readHashtable(ObjectInputStream s)
Overrides java. Perform deserialization of the Hashtable from an ObjectInputStream. |
| protected void | rehash()
Overrides java. Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently. |
| public synchronized Object | remove(Object key)
Overrides java. Implements java. Removes the key (and its corresponding value) from this hashtable. |
| public synchronized boolean | remove(Object key, Object value)
Overrides java. Overrides default java. Removes the entry for the specified key only if it is currently mapped to the specified value (optional operation). |
| public synchronized boolean | replace(Object key, Object oldValue, Object newValue)
Overrides java. Overrides default java. Replaces the entry for the specified key only if currently mapped to the specified value (optional operation). |
| public synchronized Object | replace(Object key, Object value)
Overrides java. Overrides default java. Replaces the entry for the specified key only if it is currently mapped to some value (optional operation). |
| public synchronized void | replaceAll(BiFunction<? super Object, ? super Object, ?> function)
Overrides java. Overrides default java. Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception (optional operation). |
| public void | save(OutputStream
an output stream. out, String a description of the property list. comments)
Deprecated
This method does not throw an IOException if an I/O error
occurs while saving the property list.
Calls the |
| private String | |
| public synchronized Object | Returns: the previous value of the specified key in this property list, ornull if it did not have one.the key to be placed into this property list. key, String the value corresponding to value)key.Calls the |
| public int | size()
Overrides java. Implements java. Returns the number of keys in this hashtable. |
| public void | store(Writer
an output character stream writer. writer, String a description of the property list. comments)Writes this property list (key and element pairs) in this
|
| public void | store(OutputStream
an output stream. out, String a description of the property list. comments)Writes this property list (key and element pairs) in this
|
| private void | |
| public void | storeToXML(OutputStream
the output stream on which to emit the XML document. os, String a description of the property list, or comment)null
if no comment is desired.Emits an XML document representing all of the properties contained in this table. |
| public void | storeToXML(OutputStream
the output stream on which to emit the XML document. os, String a description of the property list, or comment, String null
if no comment is desired.the name of a supported
character encoding encoding)Emits an XML document representing all of the properties contained in this table, using the specified encoding. |
| public void | storeToXML(OutputStream
the output stream on which to emit the XML document. os, String a description of the property list, or comment, Charset null
if no comment is desired.the charset charset)Emits an XML document representing all of the properties contained in this table, using the specified encoding. |
| public Set | Returns: an unmodifiable set of keys in this property list where the key and its corresponding value are strings, including the keys in the default property list.Returns an unmodifiable set of keys from this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list. |
| public synchronized String | toString()
Overrides java. Returns a string representation of this |
| public Collection | values()
Overrides java. Implements java. Returns a |
| private static void | |
| private static void | |
| pack-priv void | writeHashtable(ObjectOutputStream s)
Overrides java. Perform serialization of the Hashtable to an ObjectOutputStream. |