In this tutorial, we will show you how to read and write to/from a database.properties file.
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
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment