Posts

Showing posts with the label Java IO Package

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

Java File Writing Using FileWriter Class – Complete Beginner’s Guide

File Writing in Java What is File Writing? File Writing in Java is the process of writing data (text or characters) into a file stored on the system. Java provides the FileWriter class to perform file writing operations efficiently.   The FileWriter class is part of the java.io package and is mainly used to write character-based data into a file. FileWriter Class Package: java.io Purpose: Writes character data to a file Type: Character Output Stream FileWriter Constructor FileWriter(String filePath) throws IOException Explanation: filePath → Location where the file is created or written Throws IOException → Handles input/output errors πŸ“Œ If the file does not exist, Java creates a new file automatically. πŸ“Œ If the file exists, Java overwrites its content (unless append mode is used). FileWriter Methods Method.                                    Description write(String str).     ...