Manipulating Property Files in Java

Mauricio SilvaBlogLeave a Comment

Photo by Boskampi from pixabay.com
In configuration file hell? Take a look at Config, the easiest way to manage your application configuration across all your servers and environments.

Introduction

In this article we will use the java.util.Properties class for the manipulation of property files. We will see how to write and read in these files. This class allows us to create two types of property files, the .properties and the .xml. Both files, for example, can be seen in the settings used in Hibernate, hibernate.properties, and hibernate.cfg.xml.

One of the advantages of using property files is to separate settings that can be changed at any time in Java code. Thus, it would not be necessary to change the Java code whenever a configuration change is required.

Property files are widely used in systems developed in Java. We have internationalization settings, email settings, jdbc, logs, among others. It is up to the developer to know when it is needed.

JdbcConnection class

Let’s create a class called JdbcConnection, where we will create attributes used for JDBC type connections.

JdbcConnecion Class
public class JdbcConnection {
    private String user;
    private String pass;
    private String driver;
    private String url;
    private String dataBase;
}

.properties file

Let’s now create a test class to create and read our properties file. A property file has the following format, key, and value. To the right we have the key and the left we have the value. An example can be seen below.

jdbc.properties file
jdbc.dataBase = myDataBase
jdbc.driver = com.mysql.jdbc.Driver
jdbc.pass = admin01
jdbc.url = jdbc\:mysql\://localhost\:3306/
jdbc.user = root

We will then create the PropertiesTest class and in it we will create three methods, the main() method, and another method for creating the file and a method for reading the file.

PropertiesTest Class

import java.io.*;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) {
        JdbcConnection jdbc = new JdbcConnection();
        jdbc.setUser("root");
        jdbc.setPass("admin01");
        jdbc.setDriver("com.mysql.jdbc.Driver");
        jdbc.setUrl("jdbc:mysql://localhost:3306/");
        jdbc.setDataBase("myDataBase");
    }
}

We create an instance of the JdbcConnection class and then create an object with the information that will be part of our file.

Let’s now create the method that will generate the jdbc.properties file.

Method to create the jdbc.properties file:
private static void createProperties(JdbcConnection jdbc) {
	Properties properties = new Properties();
	properties.setProperty("jdbc.user", jdbc.getUser());
	properties.setProperty("jdbc.pass", jdbc.getPass());
	properties.setProperty("jdbc.driver", jdbc.getDriver());
	properties.setProperty("jdbc.url", jdbc.getUrl());
	properties.setProperty("jdbc.dataBase", jdbc.getDataBase());

	try {      
		FileOutputStream fos = new FileOutputStream("C:\\jdbc.properties");
		properties.store(fos, "FILE JDBC PROPERTIES:");
		fos.close();
	} catch (IOException ex) {
		System.out.println(ex.getMessage());
		ex.printStackTrace();
	}
}

We create a properties object where we set the properties through the method setProperty(). As previously explained, the first value is the key or the name of the property, and the second value is the value of the property itself.

The fos object creates the stream to write to the file defined in its constructor. The store() method does the magic of the Properties class, in it we pass as parameter the object fos and a description for the file. If you do not want to set a description, the parameter can be null.

After executing this method, we will have the following output, which can be seen below.

#FILE JDBC PROPERTIES:
#Wed Mar 30 15:48:34 BRT 2011
jdbc.url = jdbc\:mysql\://localhost\:3306/
jdbc.driver = com.mysql.jdbc.Driver
jdbc.user = root
jdbc.dataBase = myDataBase
jdbc.pass = admin01

Now let’s create a method to read the properties of this file.

private static void readPropriedades() {
	Properties properties = new Properties();

	try {
		FileInputStream fis = new FileInputStream("C:\\jdbc.properties");
		properties.load(fis);
	} catch (IOException e) {
		e.printStackTrace();
	}

	String p1 = properties.getProperty("jdbc.user");
	String p2 = properties.getProperty("jdbc.pass");
	String p3 = properties.getProperty("jdbc.driver");
	String p4 = properties.getProperty("jdbc.url");
	String p5 = properties.getProperty("jdbc.dataBase");
	System.out.println(p1 + "\n" + p2 + "\n" + p3 + "\n" + p4 + "\n" + p5);
}

To set the properties we use the setProperty() method, now to read we need to use the getProperty() method, this time let’s just select the key for the value we want to retrieve in the file.

Run the read method and see in the console that it will have an output with the values of the properties in the jdbc.properties file.

Leave a Reply

Your email address will not be published. Required fields are marked *