Package nl.colorize.util
Class PropertyUtils
java.lang.Object
nl.colorize.util.PropertyUtils
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 Summary
Modifier and TypeMethodDescriptionprotected static Properties
emulateLoadProperties
(Reader source) Custom implementation for parsing.properties
files.static Properties
filterPrefix
(Properties original, String prefix) Returns a newProperties
object that only includes properties from the original that have a name matching the specified prefix.static Properties
loadProperties
(File source) Uses the logic described inloadProperties(Reader)
to load a.properties
file using the UTF-8 character encoding.static Properties
loadProperties
(File source, Charset charset) Uses the logic described inloadProperties(Reader)
to load a.properties
file, using the specified character encoding.static Properties
loadProperties
(Reader source) Parses the contents of a.properties
file and returns the resultingProperties
object.static Properties
loadProperties
(String contents) Uses the logic described inloadProperties(Reader)
to load.properties
file contents from a string.static Properties
loadProperties
(ResourceFile file) Uses the logic described inloadProperties(Reader)
to load a.properties
file, using the UTF-8 character encoding.static Properties
loadProperties
(ResourceFile file, Charset charset) Uses the logic described inloadProperties(Reader)
to load a.properties
file, using the specified character encoding.static void
saveProperties
(Properties data, File dest) Uses the logic fromsaveProperties(Properties, Writer)
to serialize aProperties
instance to a.properties
file.static void
saveProperties
(Properties data, File dest, Charset charset) Uses the logic fromsaveProperties(Properties, Writer)
to serialize aProperties
instance to a.properties
file.static void
saveProperties
(Properties data, Writer dest) Serializes aProperties
object to the specified writer.static String
Uses the logic fromsaveProperties(Properties, Writer)
to serialize aProperties
instance to.properties
file contents.toMap
(Properties properties) Creates a map from aProperties
object.static Properties
toProperties
(Map<String, String> data) Creates aProperties
object from a map.
-
Method Details
-
loadProperties
Parses the contents of a.properties
file and returns the resultingProperties
object.This method will use a different implementation depending on the platform. On platforms that do not support the standar
Properties.load(Reader)
, such as TeaVM, a custom implementation is used in order to support UTF-8 property files.- Throws:
ResourceException
- if an I/O error occurs while reading the file.
-
emulateLoadProperties
Custom implementation for parsing.properties
files. See the documentation forloadProperties(Reader)
for more information on when and why this is used.- Throws:
IOException
-
loadProperties
Uses the logic described inloadProperties(Reader)
to load a.properties
file, using the specified character encoding.- Throws:
ResourceException
- if an I/O error occurs while reading the file.
-
loadProperties
Uses the logic described inloadProperties(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
Uses the logic described inloadProperties(Reader)
to load a.properties
file, using the specified character encoding.- Throws:
IOException
- if an I/O error occurs while reading the file.
-
loadProperties
Uses the logic described inloadProperties(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
Uses the logic described inloadProperties(Reader)
to load.properties
file contents from a string. -
saveProperties
Serializes aProperties
object to the specified writer. This method differs from the standardProperties.store(Writer, String)
, in that it writes the properties in a deterministic (alphabetical) order.- Throws:
IOException
- if an I/O error occurs while writing.
-
saveProperties
Uses the logic fromsaveProperties(Properties, Writer)
to serialize aProperties
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
Uses the logic fromsaveProperties(Properties, Writer)
to serialize aProperties
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
Uses the logic fromsaveProperties(Properties, Writer)
to serialize aProperties
instance to.properties
file contents. -
toProperties
Creates aProperties
object from a map. Anynull
keys and/or values in the map will not be registered as properties. -
toMap
Creates a map from aProperties
object. Anynull
or empty property values will not be included in the map. -
filterPrefix
Returns a newProperties
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.
-