public static void saveProperties(Map data, String filePath) {
Properties props = new Properties();
props.putAll(data);
try (FileOutputStream out = new FileOutputStream(filePath)) {
props.store(out, "Updated at " + LocalDateTime.now());
} catch (IOException e) {
throw new UncheckedIOException("寫入配置失敗", e);
}
}
進階用法
類型安全轉換
int port = Integer.parseInt(props.getProperty("server.port", "8080"));
boolean debug = Boolean.parseBoolean(props.getProperty("debug", "false"));
List hosts = Arrays.asList(props.getProperty("hosts", "").split(","));
多環境配置隔離
// application.properties (默認)
// application-dev.properties (開發)
// application-prod.properties (生產)
// Spring Boot 自動按 active profile 加載覆蓋