Syntax
properties.load(reader); properties.getProperty(key, fallback)Examples
Properties Configuration
Save as Main.java and run with a Java 17+ JDK: java Main.java.
import java.util.Properties;
import java.io.StringReader;
public class Main {
public static void main(String[] args) throws Exception {
Properties settings = new Properties();
settings.load(new StringReader("port=8080\nmode=dev\n"));
int port = Integer.parseInt(settings.getProperty("port"));
System.out.println(port + 1);
System.out.println(settings.getProperty("region", "local"));
}
}- Expected output: 8081 local
- Loaded values are strings, so the port requires conversion. The missing region uses the supplied fallback.
Best practices
- Use setProperty rather than put to maintain string keys and values.
- Validate required settings and ranges before starting application work.
Runtime & versions
Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Java Language Specification