Properties
The Properties
class represents a persistent set of properties. The Properties
can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. This class is used extensively for configuration settings, such as setting up application parameters, internationalization, and managing application settings.
A property list can contain another property list as its “defaults”; this second property list is searched if the property key is not found in the original property list.
The Properties
class extends Hashtable<Object, Object>
and is synchronized.
Constructors
Properties()
: Creates an empty property list with no default values.
Properties(Properties defaults)
: Creates an empty property list with the specified default values.
Common Methods
setProperty(String key, String value)
: Calls the put
method of the Hashtable
class and returns the previous value associated with the key.
getProperty(String key)
: Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list (if any) is checked.
getProperty(String key, String defaultValue)
: Returns the value in this property list with the specified key. If the key is not found, it returns the default value.
load(InputStream inStream)
: Reads a property list (key and element pairs) from the input byte stream.
load(Reader reader)
: Reads a property list (key and element pairs) from the input character stream.
store(OutputStream out, String comments)
: Writes this property list (key and element pairs) to the output stream, along with the specified comments.
store(Writer writer, String comments)
: Writes this property list (key and element pairs) to the output character stream, along with the specified comments.
list(PrintStream out)
: Prints a list of properties to the specified output stream.
list(PrintWriter out)
: Prints a list of properties to the specified output character stream.
import java.io.*;
import java.util.Properties;
public class PropertiesExample {
public static void main(String[] args) {
Properties properties = new Properties();
// Set properties
properties.setProperty("database.url", "jdbc:mysql://localhost:3306/mydb");
properties.setProperty("database.user", "root");
properties.setProperty("database.password", "password");
// Save properties to a file
try (OutputStream output = new FileOutputStream("config.properties")) {
properties.store(output, "Database Configuration");
} catch (IOException io) {
io.printStackTrace();
}
// Load properties from a file
Properties loadedProperties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
loadedProperties.load(input);
} catch (IOException io) {
io.printStackTrace();
}
// Access properties
String url = loadedProperties.getProperty("database.url");
String user = loadedProperties.getProperty("database.user");
String password = loadedProperties.getProperty("database.password");
// Print properties
System.out.println("Database URL: " + url);
System.out.println("Database User: " + user);
System.out.println("Database Password: " + password);
}
}
import java.util.Properties;
public class DefaultPropertiesExample {
public static void main(String[] args) {
Properties defaults = new Properties();
defaults.setProperty("timeout", "30");
defaults.setProperty("language", "en");
Properties properties = new Properties(defaults);
properties.setProperty("timeout", "60");
// Get properties with and without default values
String timeout = properties.getProperty("timeout");
String language = properties.getProperty("language");
String country = properties.getProperty("country", "US");
// Print properties
System.out.println("Timeout: " + timeout); // 60
System.out.println("Language: " + language); // en
System.out.println("Country: " + country); // US
}
}