🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
This is a common task for a developer to maintain project configuration data or settings in an external file, for example keeping JDBC database configurations in the database.properties file. In this tutorial, we will show you how to read and write to/from a database.properties file.
1. Write to Properties File Example
Let's write a Java program to set the property key and value, and save it to a specific location. In this example, we will use "database.properties" path. In the next example, we will read or load the same file.
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(); } } }
Output:
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
The "database.properties" file has created under the root directory of your project with the following content -
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
Let's read or load properties from "database.properties" file that created in the above example and print the key-value pair.
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(); } } }
Output:
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
Let's keep "database.properties" file in the classpath. Now let's read or load a properties file database.properties from project classpath, and print the key-value pair.
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; } }
Output:
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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment