Reading and Writing Properties Files in Java

Java, a versatile and widely-used programming language, offers a unique provision for handling properties files. These files are pivotal for representing configuration values, user preferences, and more. In this guide, we delve deep into the intricacies of reading and writing properties files in Java, both in XML and text formats.

graph TD A[Properties File] --> B[Key-Value Pairs] B --> C[Key] B --> D[Value] A --> E[XML or Text Format]

Understanding Java Properties Files

Java properties files are text files that store data in a key-value pair format, where the key is known as the property. These files simplify the process of accessing configuration settings by allowing developers to read the value of a property using its name. Notable applications like Spring and Spring Boot utilize properties files for configurations. For instance, application.properties is commonly employed to set up Spring Boot.

Common Use Cases

  1. JDBC Connectivity: The jdbc.properties file is frequently used to store database connection parameters such as URL, username, and password.
  2. Logging: The log4j.properties file contains settings for Java logging using the log4j framework.
  3. Frameworks: Many frameworks, including Struts, Spring, and Displaytag, rely on Java properties files.

Reading Values from Java Properties Files

Let's explore a practical example of reading a properties file in Java. In this scenario, we'll extract properties from a jdbc.properties file, which contains database connection settings.

Java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertyFileReader {

    public static void main(String args[]) throws FileNotFoundException, IOException {

        // Initialize properties object
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream("c:/jdbc.properties");
        
        // Load properties from the file
        props.load(fis);

        // Retrieve property values
        String username = props.getProperty("jdbc.username");
        String driver = props.getProperty("jdbc.driver");
        System.out.println("jdbc.username: " + username);
        System.out.println("jdbc.driver: " + driver);
    }
}

Reading Properties Files in XML Format

Java properties files can also be represented in XML format. This is especially beneficial when sharing configurations with external tools that exclusively understand XML.

Here's a sample XML properties file:

XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="jdbc.username">root</entry>
    <entry key="jdbc.password">mysql</entry>
</properties>

To read this XML properties file in Java, follow the example below:

Java
public static void main(String args[]) throws FileNotFoundException, IOException {

    // Initialize properties object
    Properties props = new Properties();
    FileInputStream fis = new FileInputStream("c:/properties.xml");
    
    // Load properties from the XML file
    props.loadFromXML(fis);

    // Retrieve property value
    String username = props.getProperty("jdbc.username");
    System.out.println("jdbc.username: " + username);
}

Conclusion

Properties files in Java, whether in text or XML format, offer a robust mechanism for managing settings and configuration data. Their linear and hierarchical representation capabilities make them a preferred choice for various applications and frameworks. By understanding and effectively utilizing properties files, developers can ensure efficient configuration management in their Java applications.

Author