📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. Write to Properties File Example
package com.javaguides.collections.properties; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class WritePropertyFile { public static void main(String[] args) { try (OutputStream output = new FileOutputStream("database.properties")) { Properties prop = new Properties(); // set the properties value prop.setProperty("jdbc.driverClassName", "com.mysql.jdbc.Driver"); prop.setProperty("jdbc.url", "jdbc:mysql://localhost:3306/demo?useSSL=false"); prop.setProperty("jdbc.username", "root"); prop.setProperty("jdbc.password", "root"); // save properties to project root folder prop.store(output, null); // Java 8 , print key and values prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value)); } catch (IOException io) { io.printStackTrace(); } } }
Key : jdbc.driverClassName, Value : com.mysql.jdbc.Driver
Key : jdbc.password, Value : root
Key : jdbc.username, Value : root
Key : jdbc.url, Value : jdbc:mysql://localhost:3306/demo?useSSL=false
jdbc.url=jdbc\:mysql\://localhost\:3306/demo?useSSL\=false
jdbc.username=root
jdbc.password=root
jdbc.driverClassName=com.mysql.jdbc.Driver
2. Read or Load a Properties File Example
package com.javaguides.collections.properties; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadPropertyFile { public static void main(String[] args) { try (InputStream input = new FileInputStream("database.properties")) { Properties prop = new Properties(); // load a properties file from InputStream prop.load(input); // Java 8 , print key and values prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value)); } catch (IOException ex) { ex.printStackTrace(); } } }
Key : jdbc.driverClassName, Value : com.mysql.jdbc.Driver
Key : jdbc.password, Value : root
Key : jdbc.username, Value : root
Key : jdbc.url, Value : jdbc:mysql://localhost:3306/demo?useSSL=false
3. Read or Load a Properties File from Classpath
package com.javaguides.collections.properties; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; public class ReadPropertyFileFromClassPath { public static void main(String[] args) { String fileName = "database.properties"; ReadPropertyFileFromClassPath obj = new ReadPropertyFileFromClassPath(); Properties prop = obj.loadProperties(fileName); // Java 8 , print key and values prop.forEach((key, value) -> System.out.println("Key : " + key + ", Value : " + value)); } public Properties loadProperties(String fileName) { Properties prop = new Properties(); try (InputStream inputStream = ReadPropertyFileFromClassPath.class.getClassLoader() .getResourceAsStream(fileName)) { // check for null if (inputStream == null) { System.out.println("Unable to find " + fileName + " file"); return prop; } // load a properties file from class path prop.load(inputStream); } catch (IOException e) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e); } return prop; } }
Key : jdbc.driverClassName, Value : com.mysql.jdbc.Driver
Key : jdbc.password, Value : root
Key : jdbc.username, Value : root
Key : jdbc.url, Value : jdbc:mysql://localhost:3306/demo?useSSL=false
Learn complete Java programming at Java Tutorial | Learn Java Programming with Examples
Comments
Post a Comment
Leave Comment