Java Properties Class
Properties inherits from Hashtable and is a class used to manage configuration information.
Since Properties inherits from the Hashtable class, it has all the functionality of Hashtable, and also provides some special methods to read and write property files.
The Properties class is often used to store program configuration information, such as database connection information, log output configuration, application settings, etc. Using the Properties class, you can store this information in a text file and read it in the program.
The Properties class is used by many Java classes. For example, when getting environment variables, it serves as the return value of the System.getProperties() method.
Properties defines the following instance variable. This variable holds a default property list associated with a Properties object.
Properties defaults;
The Properties class defines two constructor methods. The first constructor method has no default value.
Properties()
The second constructor method uses propDefault as the default value. In both cases, the property list is empty:
Properties(Properties propDefault)
In addition to the methods defined in Hashtable, Properties also defines the following methods:
| Serial number | Method description |
|---|---|
| 1 |
String getProperty(String key) Searches for the property with the specified key in this property list. |
| 2 |
String getProperty(String key, String defaultProperty) Searches for the property with the specified key in the property list. |
| 3 |
void list(PrintStream streamOut) Outputs the property list to the specified output stream. |
| 4 |
void list(PrintWriter streamOut) Outputs the property list to the specified output stream. |
| 5 |
void load(InputStream streamIn) throws IOException Reads the property list (key and element pairs) from the input stream. |
| 6 |
Enumeration propertyNames( ) Reads the property list (key and element pairs) from the input character stream in a simple line-oriented format. |
| 7 |
Object setProperty(String key, String value) Calls the put method of Hashtable. |
| 8 |
void store(OutputStream streamOut, String description) Writes the property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method. |
The Properties class provides several methods for reading and writing property files. The most commonly used methods are the load() and store() methods.
The load() method can read properties from a file, while the store() method can write properties to a file.
Below is a simple example demonstrating how to use the Properties class to read and write property files:
Example
import java.io.*;
import java.util.Properties;
public class PropertiesExample {
public static void main(String[] args) {
Properties prop = new Properties();
try {
// Read the property file
prop.load(new FileInputStream("config.properties"));
// Get the property value
String username = prop.getProperty("username");
String password = prop.getProperty("password");
// Output the property value
System.out.println("username: " + username);
System.out.println("password: " + password);
// Modify the property value
prop.setProperty("username", "newUsername");
prop.setProperty("password", "newPassword");
// Save properties to file
prop.store(new FileOutputStream("config.properties"), null);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
In the above example, we created a Properties object, and then used the load() method to read properties from the configuration file. Next, we used the getProperty() method to get the property value and output it to the console. Then, we used the setProperty() method to modify the property value, and used the store() method to save the modified properties back to the file.
Example
The following program illustrates several methods supported by this data structure:
Example
The compilation and running result of the above example is as follows:
The capital of Missouri is Jefferson City. The capital of Illinois is Springfield. The capital of Indiana is Indianapolis. The capital of California is Sacramento. The capital of Washington is Olympia. The capital of Florida is Not Found.
Other extensions
Java Data Structures