Class PropertyUtils
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 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.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) Loads a properties file from a reader 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
Loads a properties file from a reader and returns the resultingProperties
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 formuXXXX
. 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
Emulates loading.properties
files that contain non-ASCII characters, on platforms that do not support loading such files natively. The documentationloadProperties(Reader)
contains more information on how and when this is used. -
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. The writer is closed afterward.- 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.
-