Properties
About
The Properties
class in Java is a part of the java.util
package and provides a framework for managing application configuration through key-value pairs. It is particularly useful for reading and writing configuration files, especially .properties
files.
The
Properties
class is a subclass ofHashtable
that is specifically designed to handle string-based key-value pairs.Commonly used for managing application settings, localization, and configuration in Java programs.
It supports persistent storage, allowing properties to be saved to or loaded from input/output streams.
Frequently used in combination with Java's resource bundles for internationalization.
Features
Key-Value Pair Management: Stores configuration as string-based key-value pairs.
Persistent Storage: Can read from and write to files using input/output streams.
Integration with I18N: Often used for internationalization by loading localized
.properties
files.Backward Compatibility: Inherits methods from
Hashtable
, making it versatile and compatible with legacy code.Ease of Use: Simplifies reading and writing configuration properties.
Declaration
To use the Properties
class, it needs to be imported:
Methods Available
1. Loading Properties
load(InputStream inStream)
: Loads properties from an input stream.load(Reader reader)
: Loads properties from a character stream.
2. Saving Properties
store(OutputStream out, String comments)
: Writes properties to an output stream with optional comments.store(Writer writer, String comments)
: Writes properties to a character stream with optional comments.
3. Getting and Setting Properties
getProperty(String key)
: Retrieves the property value associated with the key.setProperty(String key, String value)
: Sets a property key-value pair.getOrDefault(Object key, Object defaultValue)
: Returns the property value or a default value if the key is not found.
4. Listing Properties
list(PrintStream out)
: Prints all properties to a print stream.list(PrintWriter out)
: Prints all properties to a print writer.
5. Removing and Checking Keys
remove(Object key)
: Removes the property associated with the specified key.containsKey(Object key)
: Checks if a property key exists.
6. Default Properties
A
Properties
object can be constructed with a defaultProperties
object, enabling a fallback mechanism for properties.
Usage
1. Basic Loading and Retrieving Properties
2. Writing Properties to a File
3. Using Default Properties
4. Loading Properties from Classpath
Applications and Real-World Usage
Application Configuration: Store and manage application settings, such as database configurations, API keys, and server details.
Internationalization (I18N): Load localized strings from
.properties
files for multi-language support.Default and Fallback Mechanism: Set default properties to ensure fallback values when specific properties are not provided.
Environment-Specific Configurations: Use different
.properties
files for development, staging, and production environments.Test Data: Store reusable test data or configuration for automated tests.
Last updated
Was this helpful?