Using property files in Java

configBlog, ConfigLeave a Comment

Photo by jamesosborne_ 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

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 article it will be demonstrated in a simple way how to create a properties 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 the example shown in this article the properties file was saved within the project into a folder named properties, however, the property file can be saved anywhere on your computer. However, do not forget to put your correct location in the source code of your program, because if you do not put the correct path of the file, an exception will be thrown.
In this article the data file properties.properties contains some basic information to be used to make a dummy connection to any server.

Listing 1: Property file data.properties

prop.server.host = 192.168.101.10
prop.server.password = 12345
prop.server.login = moreira

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: Class Manipulator

package test.files.properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Manipulator {
   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(“************properties file test************”);
     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 getProp () 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/data.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 / testprop / prop / data.properties”)
In this example the property file is located in “/home/testprop/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 the item prop.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.

**********properties file test**********
Login = moreira
Host = 192.168.101.10
Password = 12345

Conclusion

As you can see throughout the article, 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 *