Class FormatPreservingProperties

All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>

public class FormatPreservingProperties extends Properties
A specialization of Properties that stores its contents in the same order and with the same formatting as was used to read the contents from an input stream. This is useful because it means that a properties file loaded via this object and then written back out later on will only be different where changes or additions were made. By default, the store(OutputStream, String) method in this class does not behave the same as Properties.store(Writer, String). You can cause an instance to approximate the behavior of Properties.store(Writer, String) by invoking setDefaultEntryDelimiter(char) with =, setAddWhitespaceAfterDelimiter(boolean) with false, and setAllowDuplicates(boolean) with true. However, this will only influence how the instance will write new values, not how it will write existing key-value pairs that are modified. In conjunction with a conservative output writer, it is possible to only write to disk changes / additions. This implementation does not permit escaped ' ', '=', ':' characters in key names.
Since:
0.3.3
See Also:
  • Constructor Details

    • FormatPreservingProperties

      public FormatPreservingProperties()
    • FormatPreservingProperties

      public FormatPreservingProperties(Properties defaults)
  • Method Details

    • setDefaultEntryDelimiter

      public void setDefaultEntryDelimiter(char defaultEntryDelimiter)
      The character to use as a delimiter between property keys and values.
      Parameters:
      defaultEntryDelimiter - either ':' or '='
    • getDefaultEntryDelimiter

      public char getDefaultEntryDelimiter()
    • setAddWhitespaceAfterDelimiter

      public void setAddWhitespaceAfterDelimiter(boolean add)
      If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character). Else, this will not add a space. Default value: true. Note that Properties.store(Writer, String) never writes whitespace.
    • getAddWhitespaceAfterDelimiter

      public boolean getAddWhitespaceAfterDelimiter()
      If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character). Else, this will not add a space. Default value: true. Note that Properties.store(Writer, String) never writes whitespace.
    • setInsertTimestamp

      public void setInsertTimestamp(boolean insertTimestamp)
      If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed. Else, this will not add a timestamp. Default value: false. Note that Properties.store(Writer, String) always writes a timestamp.
    • getInsertTimestamp

      public boolean getInsertTimestamp()
      If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed. Else, this will not add a timestamp. Default value: false. Note that Properties.store(Writer, String) always writes a timestamp.
    • setAllowDuplicates

      public void setAllowDuplicates(boolean allowDuplicates)
      If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings. If set to false, duplicate property definitions in the input will cause an exception to be thrown during load(InputStream). Default value: false. Note that Properties.store(Writer, String) always allows duplicates.
    • getAllowDuplicates

      public boolean getAllowDuplicates()
      If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings. If set to false, duplicate property definitions in the input will cause an exception to be thrown during load(InputStream). Default value: false. Note that Properties.store(Writer, String) always allows duplicates.
    • getProperty

      public String getProperty(String key)
      Overrides:
      getProperty in class Properties
    • getProperty

      public String getProperty(String key, String defaultValue)
      Overrides:
      getProperty in class Properties
    • setProperty

      public Object setProperty(String key, String value)
      Overrides:
      setProperty in class Properties
    • putAll

      public void putAll(Map m)
      Circumvents the superclass putAll(Map) implementation, putting all the key-value pairs via put(Object, Object).
      Specified by:
      putAll in interface Map<Object,Object>
      Overrides:
      putAll in class Properties
    • remove

      public Object remove(Object key)
      Removes the key from the bookkeeping collectiotns as well.
      Specified by:
      remove in interface Map<Object,Object>
      Overrides:
      remove in class Properties
    • clear

      public void clear()
      Specified by:
      clear in interface Map<Object,Object>
      Overrides:
      clear in class Properties
    • clone

      public Object clone()
      Overrides:
      clone in class Properties
    • put

      public Object put(Object key, Object val)
      Specified by:
      put in interface Map<Object,Object>
      Overrides:
      put in class Properties
    • load

      public void load(InputStream in) throws IOException
      Loads the properties in in, according to the rules described in Properties.load(Reader). If getAllowDuplicates() returns true, this will throw a FormatPreservingProperties.DuplicateKeyException if duplicate property declarations are encountered.
      Overrides:
      load in class Properties
      Throws:
      IOException
      See Also:
    • save

      public void save(OutputStream out, String header)
      Overrides:
      save in class Properties
    • store

      public void store(OutputStream out, String header) throws IOException
      Overrides:
      store in class Properties
      Throws:
      IOException