Class PropertyUtils

java.lang.Object
nl.colorize.util.PropertyUtils

public final class PropertyUtils extends Object
Utility class for working with Properties and .properties files. Although .properties files are widely used in Java applications, the standard API for working with them is relatively limited. This class adds a number of convenience methods to more easily load .properties from multiple sources, to manipulate Properties instances, and to serialize the results.
  • Method Details

    • loadProperties

      public static Properties loadProperties(Reader source)
      Loads a properties file from a reader and returns the resulting Properties object.

      For .properties files containing non-ASCII characters, the behavior of this method is different depending on the platform. Originally, .properties files were required to use the ISO-8859-1 character encoding, with non-ASCII characters respresented by Unicode escape sequences in the form uXXXX. Modern versions of the .properties file format allow any character encoding to be used, but this is still not supported on all platforms, and by all Java implementations.

      This method will load the .properties file from a reader. If the current platform supports .properties files with any character encoding, the reader's character encoding is used to load the file. If the platform only supports .properties files with the ISO-8859-1 character encoding, this encoding is used as a fallback, regardless of the reader's actual character encoding. On such platforms, files that contain non-ASCII characters will not be loaded correctly.

      The reader is closed by this method after the .properties file has been loaded.

      Throws:
      ResourceException - if an I/O error occurs while reading the file.
    • emulateUnicodeProperties

      protected static void emulateUnicodeProperties(String contents, Properties properties)
      Emulates loading .properties files that contain non-ASCII characters, on platforms that do not support loading such files natively. The documentation loadProperties(Reader) contains more information on how and when this is used.
    • loadProperties

      public static Properties loadProperties(ResourceFile file, Charset charset)
      Uses the logic described in loadProperties(Reader) to load a .properties file, using the specified character encoding.
      Throws:
      ResourceException - if an I/O error occurs while reading the file.
    • loadProperties

      public static Properties loadProperties(ResourceFile file)
      Uses the logic described in loadProperties(Reader) to load a .properties file, using the UTF-8 character encoding.
      Throws:
      ResourceException - if an I/O error occurs while reading the file.
    • loadProperties

      public static Properties loadProperties(File source, Charset charset) throws IOException
      Uses the logic described in loadProperties(Reader) to load a .properties file, using the specified character encoding.
      Throws:
      IOException - if an I/O error occurs while reading the file.
    • loadProperties

      public static Properties loadProperties(File source) throws IOException
      Uses the logic described in loadProperties(Reader) to load a .properties file, using the UTF-8 character encoding.
      Throws:
      IOException - if an I/O error occurs while reading the file.
    • loadProperties

      public static Properties loadProperties(String contents)
      Uses the logic described in loadProperties(Reader) to load .properties file contents from a string.
    • saveProperties

      public static void saveProperties(Properties data, Writer dest) throws IOException
      Serializes a Properties object to the specified writer. This method differs from the standard Properties.store(Writer, String) in that it writes the properties in a deterministic (alphabetical) order. The writer is closed afterward.
      Throws:
      IOException - if an I/O error occurs while writing.
    • saveProperties

      public static void saveProperties(Properties data, File dest, Charset charset) throws IOException
      Uses the logic from saveProperties(Properties, Writer) to serialize a Properties instance to a .properties file. The file is created using the specified character encoding.
      Throws:
      IOException - if an I/O error occurs while writing the file.
    • saveProperties

      public static void saveProperties(Properties data, File dest) throws IOException
      Uses the logic from saveProperties(Properties, Writer) to serialize a Properties instance to a .properties file. The file is created using the UTF-8 character encoding.
      Throws:
      IOException - if an I/O error occurs while writing the file.
    • serializeProperties

      public static String serializeProperties(Properties data)
      Uses the logic from saveProperties(Properties, Writer) to serialize a Properties instance to .properties file contents.
    • toProperties

      public static Properties toProperties(Map<String,String> data)
      Creates a Properties object from a map. Any null keys and/or values in the map will not be registered as properties.
    • toMap

      public static Map<String,String> toMap(Properties properties)
      Creates a map from a Properties object. Any null or empty property values will not be included in the map.
    • filterPrefix

      public static Properties filterPrefix(Properties original, String prefix)
      Returns a new Properties object that only includes properties from the original that have a name matching the specified prefix.

      The property names in the result instance will have the prefix removed from their name. For example, if the original included a property "a.x", and the prefix is "a.", the result will include a property named "x".

      Throws:
      IllegalArgumentException - if the provided prefix is empty.