Commit 3096d24
committed
feat(PropertiesDemo2): demonstrate loading configuration from properties file
What
- Added PropertiesDemo2 class.
- Created java.util.Properties instance.
- Loaded key-value pairs from external file `Drink.txt` using load(FileInputStream).
- Retrieved values using getProperty("Name"), getProperty("Brand"), and getProperty("Type").
- Printed full Properties map to console.
Why
- Demonstrates how to read configuration from an external `.properties` file instead of hardcoding values.
- Useful for externalizing app settings, allowing changes without recompiling code.
- Shows typical retrieval of individual properties and printing all entries.
How
- Initialized empty Properties object.
- Used load(InputStream) to parse text file into key-value pairs.
• File format expected: `key=value` (ISO-8859-1 encoding).
- Accessed individual properties by key via getProperty().
- Printed the Properties object → outputs all key-value pairs in memory.
Logic
- Properties inherits from Hashtable<String,String>.
- Keys and values are strings only.
- load() reads data line-by-line; comments start with `#` or `!`.
- If key is missing, getProperty returns null (safe check needed in production).
- File path is absolute in this demo; can also use relative paths or classpath resources.
Real-life applications
- External config management: database URLs, API tokens, app branding.
- Easy environment-specific overrides without code changes.
- Works well for lightweight apps or small modules needing customizable settings.
- Often paired with store() and storeToXML() to persist updated settings.
Summary
- PropertiesDemo2 shows how to load `.properties` files into Java.
- Provides simple access to external configuration via key-value lookups.
- Keeps application settings flexible and maintainable.
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>1 parent 4a08682 commit 3096d24
1 file changed
0 commit comments