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 of Hashtable 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

  1. Key-Value Pair Management: Stores configuration as string-based key-value pairs.

  2. Persistent Storage: Can read from and write to files using input/output streams.

  3. Integration with I18N: Often used for internationalization by loading localized .properties files.

  4. Backward Compatibility: Inherits methods from Hashtable, making it versatile and compatible with legacy code.

  5. 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 default Properties 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

  1. Application Configuration: Store and manage application settings, such as database configurations, API keys, and server details.

  2. Internationalization (I18N): Load localized strings from .properties files for multi-language support.

  3. Default and Fallback Mechanism: Set default properties to ensure fallback values when specific properties are not provided.

  4. Environment-Specific Configurations: Use different .properties files for development, staging, and production environments.

  5. Test Data: Store reusable test data or configuration for automated tests.

Last updated