Skip to content

Commit d71d7c8

Browse files
committed
feat(ReadXMLUsingPropertieClass): add demo for reading XML configuration with Properties
What - Added ReadXMLUsingPropertieClass in Package2. - Uses java.util.Properties to load key-value pairs from an XML file. - Prints entire Properties object and retrieves value for key `"Name: "`. Why - Demonstrates how to load configuration stored in XML format. - Complements previous examples using `.properties` files and `storeToXML()`. - Useful when config needs structured storage or integration with XML tools. How - Created new Properties object. - Called loadFromXML(FileInputStream) to parse XML file into Properties. • XML must follow the DTD defined by Properties (keys/values as `<entry>` elements). - Accessed individual property using getProperty("Name: "). - Printed full Properties map to console. Logic - loadFromXML reads standard Java Properties XML format: <properties> <comment>...</comment> <entry key="Name: ">Value</entry> </properties> - Keys and values remain Strings. - If the key is not found, getProperty() returns null. - Unlike `.properties` files, encoding is always UTF-8 for XML. Real-life applications - Store user preferences, metadata, or app settings in XML form. - Easier integration with XML-based configuration systems. - Better readability and extensibility compared to flat `.properties` files. - Common in apps where configuration must be both human-readable and tool-friendly. Summary - ReadXMLUsingPropertieClass shows loading settings from an XML file into Properties. - Demonstrates retrieving both all entries and a specific key. - Highlights Properties versatility: supports both `.properties` (text) and `.xml` formats. Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>
1 parent 8bc8d84 commit d71d7c8

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
Q&A Based on `src/Package/PropertiesDemo.java` Program
2+
3+
----------------------
4+
### Basic Questions
5+
----------------------
6+
7+
**1. What is the `Properties` class in Java?**
8+
- The `Properties` class is part of `java.util`.
9+
- It extends `Hashtable<Object,Object>` but is specialized to handle **String key-value pairs**.
10+
- It is commonly used to store configuration data and metadata.
11+
12+
---
13+
14+
**2. How does the `setProperty()` method work in Java?**
15+
- `setProperty(String key, String value)` inserts or updates a key-value pair.
16+
- Internally, it calls `put(key, value)` of `Hashtable`.
17+
- If the key already exists, its value is overwritten.
18+
19+
---
20+
21+
**3. Why is the `Properties` class commonly used in Java applications?**
22+
- Provides a simple way to store **configuration settings** (like DB URLs, app configs).
23+
- Can **persist data** to `.properties` or `.xml` files.
24+
- Supports **easy loading and retrieval** using `getProperty()`.
25+
26+
---
27+
28+
**4. What is the difference between `store()` and `storeToXML()` methods?**
29+
- `store(OutputStream out, String comments)` → Saves data in plain text `.properties` format.
30+
- `storeToXML(OutputStream out, String comment)` → Saves data in structured XML format.
31+
32+
---
33+
34+
**5. What type of data does the `Properties` class store?**
35+
- Only **String keys and String values**.
36+
- Attempting to insert other object types is not supported via `setProperty()`.
37+
38+
---
39+
40+
----------------------
41+
### Intermediate Questions
42+
----------------------
43+
44+
**6. How does `store(OutputStream out, String comments)` save data to a file?**
45+
- Iterates through all key-value pairs.
46+
- Writes them in the format:
47+
48+
Comment (optional)
49+
50+
Timestamp
51+
52+
key=value
53+
54+
- Example: `OS=Windows`
55+
56+
---
57+
58+
**7. What is the structure of a `.properties` file?**
59+
- Plain text with `key=value` pairs.
60+
- Lines starting with `#` are comments.
61+
- Example:
62+
63+
Application Settings
64+
65+
username=admin
66+
password=1234
67+
68+
---
69+
70+
**8. How does `storeToXML()` format the properties in an XML file?**
71+
- Saves properties inside `<properties>` root.
72+
- Each entry is stored as `<entry key="...">value</entry>`.
73+
- Example:
74+
```xml
75+
<properties>
76+
<comment>Config File</comment>
77+
<entry key="OS">Windows</entry>
78+
</properties>
79+
80+
81+
82+
83+
9. What happens if a property key contains spaces or special characters?
84+
• In .properties files, spaces and : must be escaped (e.g., Brand\ Name=Dell).
85+
• In XML, the key is safely stored as an attribute (key="Brand Name").
86+
87+
88+
89+
10. How does the Properties class differ from HashMap?
90+
• Properties is a subclass of Hashtable.
91+
• Restricted to String keys and values.
92+
• Provides built-in methods like load(), store(), storeToXML() for persistence.
93+
• HashMap is generic and does not support direct persistence.
94+
95+
96+
97+
98+
99+
Advanced Questions
100+
101+
102+
103+
11. Can we load properties from a file instead of setting them manually? How?
104+
• Yes, using load(InputStream in) for .properties files.
105+
• Using loadFromXML(InputStream in) for XML files.
106+
107+
Properties p = new Properties();
108+
p.load(new FileInputStream("config.properties"));
109+
110+
111+
112+
113+
114+
12. What happens if we try to access a property that does not exist?
115+
• getProperty(key) returns null.
116+
• You can provide a default value:
117+
getProperty("db", "defaultDB").
118+
119+
120+
121+
13. How can we retrieve values from a Properties object?
122+
• Using getProperty(String key).
123+
• Example:
124+
125+
String os = p.getProperty("OS");
126+
127+
128+
129+
130+
131+
14. Why is Properties preferred for configuration management over a regular text file?
132+
• Provides structured access (key-value format).
133+
• Supports loading and saving with minimal code.
134+
• Built-in support for both text and XML formats.
135+
• Reduces errors compared to parsing plain text manually.
136+
137+
138+
139+
15. Can we store non-string values in a Properties object? Why or why not?
140+
• No, not directly.
141+
• setProperty() enforces String values.
142+
• If you want to store other data types (e.g., int, boolean), you must convert to String and parse back later.
143+
144+
Example:
145+
146+
props.setProperty("port", String.valueOf(8080));
147+
int port = Integer.parseInt(props.getProperty("port"));
148+
149+
150+
151+
This Q&A set covers Basic → Intermediate → Advanced levels:
152+
• Basic: class definition, storage, usage.
153+
• Intermediate: file formats, escaping, differences.
154+
• Advanced: loading, defaults, type handling, best practices.
155+
156+
---

0 commit comments

Comments
 (0)