Class Properties

All Implemented Interfaces:
Map<String,String>

public class Properties extends HashMap<String,String>

A Properties object is a Hashtable where the keys and values must be Strings. Each property can have a default Properties list which specifies the default values to be used when a given key is not found in this Properties instance.

Character Encoding

Note that in some cases Properties uses ISO-8859-1 instead of UTF-8. ISO-8859-1 is only capable of representing a tiny subset of Unicode. Use either the loadFromXML/storeToXML methods (which use UTF-8 by default) or the load/store overloads that take an OutputStreamWriter (so you can supply a UTF-8 instance) instead.

See also
  • Hashtable

  • java.lang.System#getProperties

  • Nested Class Summary

    Nested classes/interfaces inherited from class AbstractMap

    AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
    Modifier and Type
    Class
    Description
    static class 
    A key-value mapping.
    static class 
    An immutable key-value mapping.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Properties
    The default values for keys not found in this Properties instance.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Properties object.
    Properties(Properties properties)
    Constructs a new Properties object using the specified default Properties.
  • Method Summary

    Modifier and Type
    Method
    Description
    Searches for the property with the specified name.
    getProperty(String name, String defaultValue)
    Searches for the property with the specified name.
    void
    Loads properties from the specified InputStream, assumed to be ISO-8859-1.
    void
    Loads properties from the specified Reader.
    Returns all of the property names (keys) in this Properties object.
    void
    save(OutputStream out, String comment)
    Deprecated.
    This method ignores any IOException thrown while writing -- use #store instead for better exception handling.
    setProperty(String name, String value)
    Maps the specified key to the specified value.
    void
    store(OutputStream out, String comment)
    Stores properties to the specified OutputStream, using ISO-8859-1.
    void
    store(Writer writer, String comment)
    Stores the mappings in this Properties object to out, putting the specified comment at the beginning.
    Returns those property names (keys) in this Properties object for which both key and value are strings.

    Methods inherited from class HashMap

    clear, containsKey, containsValue, entrySet, get, isEmpty, keySet, put, putAll, remove, size, values
    Modifier and Type
    Method
    Description
    void
    Removes all mappings from this hash map, leaving it empty.
    boolean
    Returns whether this map contains the specified key.
    boolean
    Returns whether this map contains the specified value.
    Returns a set containing all of the mappings in this map.
    get(Object key)
    Returns the value of the mapping with the specified key.
    boolean
    Returns whether this map is empty.
    Returns a set of the keys contained in this map.
    put(String key, String value)
    Maps the specified key to the specified value.
    void
    putAll(Map<? extends String, ? extends String> map)
    Copies all the mappings in the specified map to this map.
    Removes the mapping with the specified key from this map.
    int
    Returns the number of elements in this map.
    Returns a collection of the values contained in this map.

    Methods inherited from class AbstractMap

    equals, hashCode, toString
    Modifier and Type
    Method
    Description
    boolean
    equals(Object object)
    Compares the specified object to this instance, and returns true if the specified object is a map and both maps contain the same mappings.
    int
    Returns the hash code for this object.
    Returns the string representation of this map.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
     
    final Class
    Returns the runtime class of an object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    final void
    Causes current thread to wait until another thread invokes the method or the method for this object.
    final void
    wait(long timeout)
    Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
    final void
    wait(long timeout, int nanos)
    Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
  • Field Details

    • defaults

      protected Properties defaults
      The default values for keys not found in this Properties instance.
  • Constructor Details

    • Properties

      public Properties()
      Constructs a new Properties object.
    • Properties

      public Properties(Properties properties)

      Constructs a new Properties object using the specified default Properties.

      Parameters
      • properties: the default Properties.
  • Method Details

    • getProperty

      public String getProperty(String name)

      Searches for the property with the specified name. If the property is not found, the default Properties are checked. If the property is not found in the default Properties, null is returned.

      Parameters
      • name: the name of the property to find.
      Returns

      the named property value, or null if it can't be found.

    • getProperty

      public String getProperty(String name, String defaultValue)

      Searches for the property with the specified name. If the property is not found, it looks in the default Properties. If the property is not found in the default Properties, it returns the specified default.

      Parameters
      • name: the name of the property to find.

      • defaultValue: the default value.

      Returns

      the named property value.

    • load

      public void load(InputStream in) throws IOException

      Loads properties from the specified InputStream, assumed to be ISO-8859-1. See "Character Encoding".

      Parameters
      • in: the InputStream
      Throws
      • IOException
      Throws:
      IOException
    • load

      public void load(Reader in) throws IOException

      Loads properties from the specified Reader. The properties file is interpreted according to the following rules:

      • Empty lines are ignored.

      • Lines starting with either a "#" or a "!" are comment lines and are ignored.

      • A backslash at the end of the line escapes the following newline character ("\r", "\n", "\r\n"). If there's whitespace after the backslash it will just escape that whitespace instead of concatenating the lines. This does not apply to comment lines.

      • A property line consists of the key, the space between the key and the value, and the value. The key goes up to the first whitespace, "=" or ":" that is not escaped. The space between the key and the value contains either one whitespace, one "=" or one ":" and any amount of additional whitespace before and after that character. The value starts with the first character after the space between the key and the value.

      • Following escape sequences are recognized: \ , \\, \r, \n, \!, \#, \t, \b, \f, and ￿ (unicode character).

      Parameters
      • in: the Reader
      Throws
      • IOException
      Since

      1.6

      Throws:
      IOException
    • propertyNames

      public Enumeration<?> propertyNames()
      Returns all of the property names (keys) in this Properties object.
    • stringPropertyNames

      public Set<String> stringPropertyNames()

      Returns those property names (keys) in this Properties object for which both key and value are strings.

      Returns

      a set of keys in the property list

      Since

      1.6

    • save

      @Deprecated public void save(OutputStream out, String comment)
      Deprecated.
      This method ignores any IOException thrown while writing -- use #store instead for better exception handling.

      Saves the mappings in this Properties to the specified OutputStream, putting the specified comment at the beginning. The output from this method is suitable for being read by the #load(InputStream) method.

      Parameters
      • out: the OutputStream to write to.

      • comment: the comment to add at the beginning.

      Throws
      • ClassCastException: @throws ClassCastException if the key or value of a mapping is not a String.
      Deprecated
    • setProperty

      public Object setProperty(String name, String value)

      Maps the specified key to the specified value. If the key already exists, the old value is replaced. The key and value cannot be null.

      Parameters
      • name: the key.

      • value: the value.

      Returns

      the old value mapped to the key, or null.

    • store

      public void store(OutputStream out, String comment) throws IOException

      Stores properties to the specified OutputStream, using ISO-8859-1. See "Character Encoding".

      Parameters
      • out: the OutputStream

      • comment: an optional comment to be written, or null

      Throws
      • IOException

      • ClassCastException: if a key or value is not a string

      Throws:
      IOException
    • store

      public void store(Writer writer, String comment) throws IOException

      Stores the mappings in this Properties object to out, putting the specified comment at the beginning.

      Parameters
      • writer: the Writer

      • comment: an optional comment to be written, or null

      Throws
      • IOException

      • ClassCastException: if a key or value is not a string

      Since

      1.6

      Throws:
      IOException