Java Properties File Example

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 the "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 the "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

Comments