Properties file in java

In Java, configurations are stored in properties file. Properties files are text files stored with the “.properties” extension. Properties files will have configuration in key=value format in each line. properties file can be part of jar or can be externalized. Re-compilation is not needed if any configuration is altered in the properties file.



How to read a properties file

Properties file can be read from the base directory using relative path or using absolute path. In the below example, config1.properties  is read from the base directory. The file content is read using load method in Properties class.

PropertyFile/config1.properties

buildversion=1.0.0
installversion=2.0.0

PropertyFile/src/com/yawintutor/PropertyFileReadFromFile.java

package com.yawintutor;

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

public class PropertyFileReadFromFile {
	public static void main(String[] args) {
		load("config1.properties");
	}

	public static void load(String fileName) {
		FileInputStream fis;
		try {
			fis = new FileInputStream(new File(fileName));
			Properties properties = new Properties();
			properties.load(fis);

			Enumeration<?> enumKey = properties.propertyNames();
			while (enumKey.hasMoreElements()) {
				String key = (String) enumKey.nextElement();
				String value = properties.getProperty(key);
				System.out.println("Key : " + key + ", Value : " + value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Output

Key : buildversion, Value : 1.0.0
Key : installversion, Value : 2.0.0


How to read a properties file from class path

The properties file will be placed in the same package where the class file is available. By default,  getResourceAsStream method picks the properties file from the same package.

Propertyfile/src/com/yawintutor/config2.properties

buildversion=1.0.0
installversion=2.0.0

Propertyfile/src/com/yawintutor/PropertyFileReadFromClassPath.java

package com.yawintutor;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropertyFileReadFromClassPath {
	public static void main(String[] args) {
		System.out.println("\nproperties file loading from current folder");
		load("config2.properties");
	}

	public static void load(String name) {
		InputStream is;
		try {
			is = PropertyFileReadFromClassPath.class.getResourceAsStream(name);
			Properties properties = new Properties();
			properties.load(is);

			Enumeration<?> enumKey = properties.propertyNames();
			while (enumKey.hasMoreElements()) {
				String key = (String) enumKey.nextElement();
				String value = properties.getProperty(key);
				System.out.println("Key : " + key + ", Value : " + value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Output

properties file loading from current folder
Key : buildversion, Value : 1.0.0
Key : installversion, Value : 2.0.0


How to read a properties file from class path, placed in different package

If the properties file is available in the different package of the calling class,  it is necessary to specify the absolute package name. In the below example, the property file is available in com.yawintutor.file package. The file path should be like “/com/yawintutor/file/config3.properites”.

Propertyfile/src/com/yawintutor/file/config3.properties

buildversion=1.0.0
installversion=2.0.0

Propertyfile/src/com/yawintutor/PropertyFileReadFromClassPath2.java

package com.yawintutor;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropertyFileReadFromClassPath2 {
	public static void main(String[] args) {
		System.out.println("properties file loading using package name");
		load("/com/yawintutor/file/config3.properties");

	}

	public static void load(String name) {
		InputStream is;
		try {
			is = PropertyFileReadFromClassPath2.class.getResourceAsStream(name);
			Properties properties = new Properties();
			properties.load(is);

			Enumeration<?> enumKey = properties.propertyNames();
			while (enumKey.hasMoreElements()) {
				String key = (String) enumKey.nextElement();
				String value = properties.getProperty(key);
				System.out.println("Key : " + key + ", Value : " + value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Output

properties file loading using package name
Key : buildversion, Value : 1.0.0
Key : installversion, Value : 2.0.0


How to create a properties file / write in a properties file

Properties class enables configurations to store in file using store method. set all key value pair in property class and use store method to write in file.

PropertyFile/src/com/yawintutor/CreatePropertyFile.java

package com.yawintutor;

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

public class CreatePropertyFile {
	public static void main(String[] args) {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(new File("config4.properties"));
			Properties properties = new Properties();
			properties.put("buildversion", "1.0.0");
			properties.put("installversion", "2.0.0");

			properties.store(fos, "test file");
			System.out.println("Properties file is created successfully");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

Output

Properties file is created successfully

PropertiesFile/config4.properties

#test file
#Wed Jul 31 22:25:44 IST 2019
buildversion=1.0.0
installversion=2.0.0



Related Articles

Leave a Reply

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