//ApplicationProperties Class // 1. Creating and Writing to an Attribute File: // a. Create a Properties object. // b. Use setProperty(String key, String value) to add key-value pairs. // c. Create an OutputStream (e.g., FileOutputStream) to the target file. // d. Call store(OutputStream out, String comments) to write the properties to the file import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class ApplicationProperties { public void writeAttributes() { Properties props = new Properties(); props.setProperty("database.url", "jdbc:mysql://localhost:3306/mydb"); props.setProperty("database.username", "user"); props.setProperty("database.password", "pass"); try (FileOutputStream fos = new FileOutputStream("config.properties")) { props.store(fos, "Application Configuration"); System.out.println("Properties saved to config.properties"); } catch (IOException e) { e.printStackTrace(); } } public void readAttributes() { // 2. Reading Properties File: // a. Create a Properties object. // b. Create an InputStream (e.g., FileInputStream) from the attribute file. // c. Call load(InputStream in) to load the properties from the file. // d. Use getProperty(String key) to retrieve the value associated with a key. Properties props = new Properties(); try (FileInputStream fis = new FileInputStream("config.properties")) { props.load(fis); String dbUrl = props.getProperty("database.url"); String dbUsername = props.getProperty("database.username"); String dbPassword = props.getProperty("database.password"); String textFilePath = props.getProperty("new.key"); System.out.println("Database URL: " + dbUrl); System.out.println("Database Username: " + dbUsername); System.out.println("Database Password: " + dbPassword); System.out.println("NEW KEY VALUE: " + textFilePath); } catch (IOException e) { e.printStackTrace(); } } public void addNewAttribute() { String filePath = "config.properties"; // Name of your properties file Properties properties = new Properties(); // 1. Load the existing properties file try (FileInputStream fis = new FileInputStream(filePath)) { properties.load(fis); } catch (IOException e) { System.err.println("Error loading properties file: " + e.getMessage()); // If the file doesn't exist, a new one will be created when saving } // 2. Add a new key-value pair properties.setProperty("new.key", "new_value_added"); properties.setProperty("another.setting", "true"); // Example of adding another // 3. Save the updated properties back to the file try (FileOutputStream fos = new FileOutputStream(filePath)) { properties.store(fos, "Updated properties file"); System.out.println("New value added and properties saved successfully."); } catch (IOException e) { System.err.println("Error saving properties file: " + e.getMessage()); } } public void readTextFileAttribute() { // 2. Reading Properties File: // a. Create a Properties object. // b. Create an InputStream (e.g., FileInputStream) from the attribute file. // c. Call load(InputStream in) to load the properties from the file. // d. Use getProperty(String key) to retrieve the value associated with a key. Properties props = new Properties(); try (FileInputStream fis = new FileInputStream("config.properties")) { props.load(fis); String dbUrl = props.getProperty("database.url"); String dbUsername = props.getProperty("database.username"); String dbPassword = props.getProperty("database.password"); String textFilePath = props.getProperty("new.key"); System.out.println("Database URL: " + dbUrl); System.out.println("Database Username: " + dbUsername); System.out.println("Database Password: " + dbPassword); System.out.println("NEW KEY VALUE: " + textFilePath); } catch (IOException e) { e.printStackTrace(); } } } end///////////////////////////////// start////////////////////////////////////////////////////////////////////////////// 1. Save the properties to the properties file Set the property key and value, and save it somewhere. SaveProperties.java package com.mkyong; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class SaveProperties { public static void main(String[] args) { try (OutputStream output = new FileOutputStream("path/to/config.properties")) { Properties prop = new Properties(); // set the properties value prop.setProperty("db.url", "localhost"); prop.setProperty("db.user", "mkyong"); prop.setProperty("db.password", "password"); // save properties to project root folder prop.store(output, null); System.out.println(prop); } catch (IOException io) { io.printStackTrace(); } } } // #Thu Apr 11 17:37:58 SRET 2019 db.user=mkyong db.password=password db.url=localhost ......////////////////////////////////////////////////////////////////////////////////// 2. Load a properties file Load a properties file from the file system and retrieve the property values. LoadProperties.java package com.mkyong; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProperties { public static void main(String[] args) { try (InputStream input = new FileInputStream("path/to/config.properties")) { Properties prop = new Properties(); // load a properties file prop.load(input); // get the property value and print it out System.out.println(prop.getProperty("db.url")); System.out.println(prop.getProperty("db.user")); System.out.println(prop.getProperty("db.password")); } catch (IOException ex) { ex.printStackTrace(); } } } Copy Output localhost mkyong password end///////////////////////////////////////////////////// start/////////////////////////////////////////////////// 4. Prints everything from a properties file Load a properties file config.properties from project classpath, and print out the keys and values. App4.java package com.mkyong; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Set; public class App4 { public static void main(String[] args) { App4 app = new App4(); app.printAll("config.properties"); } private void printAll(String filename) { try (InputStream input = getClass().getClassLoader().getResourceAsStream(filename)) { Properties prop = new Properties(); if (input == null) { System.out.println("Sorry, unable to find " + filename); return; } prop.load(input); // Java 8 , print key and values prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value)); // Get all keys prop.keySet().forEach(x -> System.out.println(x)); Set objects = prop.keySet(); /*Enumeration e = prop.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = prop.getProperty(key); System.out.println("Key : " + key + ", Value : " + value); }*/ } catch (IOException ex) { ex.printStackTrace(); } } } Copy Output Key : db.user, Value : mkyong Key : db.password, Value : password Key : db.url, Value : localhost db.user db.password db.url end////////////////////////////////////////////////////////