Posts

Showing posts with the label Java Properties Class

How to Read a Properties File in Java: Step-by-Step Explanation

📘 Reading a Properties File in Java In Java, a properties file is commonly used to store configuration data such as database credentials in the form of key–value pairs. To read this file, Java provides the Properties class. 🧩 Steps to Read a Properties File 🔹 Step 1: Create a Properties Object Create an object of the Properties class. This object contains: load() method Overloaded getProperty() methods Properties props = new Properties(); 🔹 Step 2: Create a FileReader Object Create an object of the FileReader class by passing the file path of the properties file. FileReader fr = new FileReader("config.properties"); 📌 When the FileReader object is created, the operating system opens an input stream to read the file. 🔹 Step 3: Load the Properties File Use the load() method to read key–value pairs from the file and store them inside the Properties object. props.load(fr) ; 🔹 Step 4: Read Values Using getProperty() Use the getProperty() method to retrieve values using keys...

Properties File in Java: Configuration, Reading Data, and Methods Explained

📘 Properties File in Java A Properties file is a configuration file used to store application settings—most commonly database credentials—in the form of key-value pairs. It helps separate configuration data from application logic, making the application easier to maintain and modify. 🔑 Format of a Properties File Properties db.url=jdbc:mysql://localhost:3306/testdb db.username=root db.password=admin 📦 Reading a Properties File in Java To read data from a properties file, Java provides the Properties class, which is available in the java.util package. Required Packages: java.util java.io 🧩 Steps to Read a Properties File Create an object of the Properties class Open the file using FileReader Load the key-value pairs into the Properties object Retrieve values using keys 🔧 Important Methods of Properties Class 🔹 load() Method Used to load key-value pairs from a file into the Properties object. void load(FileReader fr) throws IOException 📌 This method belongs to the Properties cla...