Managing Application Properties with java.util.Properties in Java


The Properties class is a convenient way to manage application settings and configurations, supporting easy loading and saving of key-value pairs.

Source Code

Properties props = new Properties();
props.setProperty("username", "user1");
props.setProperty("password", "passwd");

// Save properties to a file
try (OutputStream out = new FileOutputStream("config.properties")) {
    props.store(out, "Application Settings");
} catch (IOException e) {
    e.printStackTrace();
}

// Load properties from a file
try (InputStream in = new FileInputStream("config.properties")) {
    props.load(in);
    System.out.println("Username: " + props.getProperty("username"));
} catch (IOException e) {
    e.printStackTrace();
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments