Posts

Showing posts with the label Java FileReader Example

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...