Using configuration files in Java

configBlog, ConfigLeave a Comment

Photo by vintuitive from flickr.com
According to the Tiobe Programming Index, Java has been the topmost popular language since its creation in the mid-90’s. If you want to know how to create and use a properties configuration file, read on!

Introduction

The property file is a great option to pass settings to a particular application that needs external settings and the same can not be changed. An example would be a program that connects to a database and needs data to make the connection, without changing the source code. Another example would be to store information that a particular .jar file can use.

In this post it will be demonstrated in a simple way how to create a properties/config file and make a program created in Java read the same and display its contents in the user console.

Create the property file

The property file is nothing more than a simple file with .properties extension. The same must be saved in a predetermined place and your Java program knows where it is so that it can be read.

In this post the data file properties.properties contains some basic information to be used to make a connection to a server.

Listing 1: Property file data.properties

prop.server.host = 195.162.100.10
prop.server.password = 1234
prop.server.login = mauricio

Create a program to read the property file

To read the properties file, a simple program was created that opens the file and captures the information contained in it, making the display of its contents on the console.

Listing 2: PropertyTest Class

package example.moreira.mauricio;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertyTest {

	public static Properties getProp() throws IOException {
		Properties props = new Properties();
		FileInputStream file = new FileInputStream("./properties/data.properties");
		props.load(file);
		return props;

	}

	public static void  main(String args[]) throws IOException {
		String login;
		String host;
		String password;
		System.out.println("************Reading test property file************");
		
		Properties prop = getProp();
		
		login = prop.getProperty("prop.server.login");
		host = prop.getProperty("prop.server.host");
		password = prop.getProperty("prop.server.password");
		
		System.out.println("Login = " + login);
		System.out.println("Host = " + host);
		System.out.println("Password = " + password);
	}
}

The getPropt () method:

Properties props = new Properties (); – Instantiate the Properties class in the variable props.

FileInputStream file = new FileInputStream (“./properties/data.properties”); – Adds the .properties file to the variable file.

props.load (file) – Loads the properties file in the props variable.

If it were necessary to obtain a file of properties outside the project itself, it would be necessary to only inform the location of the same in the section below:

FileInputStream file = new FileInputStream (“./ properties/data.properties”)

It is only necessary to change the ./properties/dados.properties to the location where the properties file is.

Below you can see an example of the code snippet that is indicating the properties file in a different location.

FileInputStream file = new FileInputStream (“/home/mauricio/prop/data.properties”)

In this example the property file is located in “/home/mauricio/prop/data.properties”.

The main() method:

Properties prop = getProp (); – An object of type Properties is instantiated in the variable prop.

login = prop.getProperty (“prop.server.login”); – Get the property of the item prop.server.login and assign its value to the login variable.

host = prop.getProperty (“prop.server.host”); – It takes the property of the item prop.server.host and assigns the value of it to the host variable.

password = prop.getProperty (“prop.server.password”); – It takes the property of itemprop.server.host and assigns the value of it to the password variable.

System.out.println (“Login =” + login); – Displays the contents of the login variable in the console.

System.out.println (“Host =” + host); – Displays the contents of the host variable on the console.

System.out.println (“Password =” + password); – Displays the contents of the password variable on the console.

Result of program execution

When the program runs, the values contained in the data.properties property file will be retrieved and they will be demonstrated in the console through the System.out.println command.

Listing 3: Result in the execution console of the program.

************Reading test property file************
Login = mauricio
Host = 195.162.100.10
Password = 1234

Conclusion

As you can see throughout the post, the use of property files can be very useful for programs that rely on external settings for their operation, such as a program that performs database query and needs basic information to connect to it.

Leave a Reply

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