The Properties class in Java is used to store and manage configuration data in the form of key–value pairs. It is commonly used for application settings, database configuration, and environment-specific values.
java.util packageHashtable<Object, Object>.properties configuration filesA Properties object works like a map where both keys and values are strings. It provides helper methods such as load(), store(), getProperty(), and setProperty().
Simulate the setProperty() method. Add keys and values to see how they are stored in the Object vs. a File.
// Demonstrates basic usage of Java Properties class
import java.util.Properties;
public class DemoProperties {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("username", "admin");
props.setProperty("password", "secret123");
String user = props.getProperty("username");
String pass = props.getProperty("password");
System.out.println("Username: " + user);
System.out.println("Password: " + pass);
}
}
The program prints the stored configuration values:
This shows how setProperty() and getProperty() are used to manage application settings in memory.
// Loads configuration values from a .properties file
import java.io.FileInputStream;
import java.util.Properties;
public class LoadPropertiesFile {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
props.load(fis);
System.out.println(props.getProperty("db.url"));
System.out.println(props.getProperty("db.user"));
}
}
db.url or app.modeconfig.properties file and load values from itgetProperty(key, defaultValue)store()