Codectionary / Developer documentation / Java

Properties Configuration

Properties reads and stores string-valued configuration, with defaults for absent keys. Reader-based loading lets the caller choose character decoding. Save as Main.java and run with a Java 17+ JDK: java Main.java.

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"));
  }
}
  1. Expected output: 8081 local
  2. 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

Specifications & further reading