Java - Properties file example

Posted on November 12, 2016


In this post, we will learn how to read and write properties file in java.  A properties file is used as configuration file in java application, which stores key and its corresponding value.  

A sample properties file currency_codes.properties is shown below.

currency_codes.properties

USD=United States dollar
EUR=Euro
GBP=Pound sterling
CNY=Chinese yuan
INR=Indian rupee
JPY=Japanese yen
RUB=Russian ruble

 

Creating properties file

A java.util.Properties class stores key and its value in list as string. The store() method of the Properties class writes property list (key value pair) to output stream. The following is an example to demonstrate this.

PropertiesWriteExample.java

package com.boraji.tutorial.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author imssbora
 */
public class PropertiesWriteExample {
	public static void main(String[] args) {
		File file = new File("config_mail.properties");

		Properties prop = null;
		FileOutputStream fileOutputStream = null;
		try {
			prop = new Properties();
			fileOutputStream = new FileOutputStream(file);

			// Add key-value elements to properties list
			prop.setProperty("mail.from", "[email protected]");
			prop.setProperty("mail.to", "[email protected]");
			prop.setProperty("mail.smtp", "10.10.10.10");
			prop.setProperty("mail.smtp.port", "250");

			// Write properties list to output stream
			prop.store(fileOutputStream, "This is an sample properties file.");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Output file (config_mail.properties)

#This is an sample properties file.
#Sat Nov 12 22:35:54 IST 2016
mail.smtp=10.10.10.10
mail.smtp.port=250
[email protected]
[email protected]

 

Reading properties file

The load() method of the Properties class reads property list from input stream. In the following example, we will show you how to read config_mail.properties.

PropertiesReadExample.java

package com.boraji.tutorial.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author imssbora
 */
public class PropertiesReadExample {
	public static void main(String[] args) {
		File file = new File("config_mail.properties");

		Properties prop = null;
		FileInputStream fileInputStream = null;
		try {
			prop = new Properties();
			fileInputStream = new FileInputStream(file);

			// Load property list
			prop.load(fileInputStream);

			// Print property list
			System.out.println("mail.from=" + prop.getProperty("mail.from"));
			System.out.println("mail.to=" + prop.getProperty("mail.to"));
			System.out.println("mail.smtp=" + prop.getProperty("mail.smtp"));
			System.out.println("mail.smtp.port=" + prop.getProperty("mail.smtp.port"));

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileInputStream != null) {
					fileInputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Output

[email protected]
[email protected]
mail.smtp=10.10.10.10
mail.smtp.port=250