Java : Load Properties files

By | October 31, 2014

In this post we will know how to load properties files in java. Properties files are amazing resources to add information. In General these files are used to store static information. We add the information in key and value pair. The values which we do not want to hard code in our Java code can be maintained in the properties files.

In general we use to store database connection details, log4j details in the properties files.

The properties files became a part of the deployable code (e.g. JAR file) making it easy to manage.
Properties files can be loaded independent of the path of source code.

There are multiple ways of loading properties files in Java.
1. Using FileInputStream
2. Using ClassLoader.getResourceAsStream()
3. Using Class.getResourceAsStream()

Create a project in eclipse and add the properties files as shown below.

load properties
The Type 1 uses the normal java File Object to read the Properties file and using FileInputStream we load the properties file.

public Properties loadPropertiesType1(){
		Properties props = new Properties();
		try{
		   File propsFile = new File(PROPERTIES_FILE);
		   props.load(new FileInputStream(propsFile));
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return props;
	}

The Type 2 using the ClassLoader.getResourceAsStream() for this we need to create a resource folder same as the source folder to work. Means is our properties file in some folder we use this without specifying the folder name in the path.


 public Properties loadPropertiesType2(){
		Properties props = new Properties();
		try{
	           InputStream in = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
		   props.load(in);
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return props;
	}

The Type 3 using the Class.getResourceAsStream() if properties file is in some package.

 public Properties loadPropertiesType3(){
		Properties props = new Properties();
		try{
		   InputStream in = this.getClass().getResourceAsStream("/com/test/"+PROPERTIES_FILE);
		   props.load(in);
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return props;
	}

The complete java code for loading the properties file is shown below.

package com.example;

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


public class LoadProperties {

	public static final String PROPERTIES_FILE = "my.properties";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub\
		LoadProperties loadProps = new LoadProperties();
		Properties props1 = new Properties();
		Properties props2 = new Properties();
		Properties props3 = new Properties();
		
		props1 = loadProps.loadPropertiesType1();
		System.out.println("=======Load Properties Type 1=======");
		System.out.println("Name : " + props1.getProperty("my.name"));
		System.out.println("Password : " + props1.getProperty("my.password"));
		
		System.out.println("");
		props2 = loadProps.loadPropertiesType2();
		System.out.println("=======Load Properties Type 2=======");
		System.out.println("Name : " + props2.getProperty("my.name"));
		System.out.println("Password : " + props2.getProperty("my.password"));
		
		System.out.println("");
		props3 = loadProps.loadPropertiesType3();
		System.out.println("=======Load Properties Type 3=======");
		System.out.println("Name : " + props3.getProperty("my.name"));
		System.out.println("Password : " + props3.getProperty("my.password"));
		

	}
	
	public Properties loadPropertiesType1(){
		
		Properties props = new Properties();
		
		try{
			
			File propsFile = new File(PROPERTIES_FILE);
			props.load(new FileInputStream(propsFile));
			
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return props;
	}
	
	public Properties loadPropertiesType2(){
		
		Properties props = new Properties();
		
		try{
			
			InputStream in = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
			props.load(in);
			
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return props;
	}

	public Properties loadPropertiesType3(){
			
		Properties props = new Properties();
		
		try{
			
			InputStream in = this.getClass().getResourceAsStream("/com/test/"+PROPERTIES_FILE);
			props.load(in);
			
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		return props;
	}
}

The output for the above code :


=======Load Properties Type 1=======
Name : Test
Password : 12345

=======Load Properties Type 2=======
Name : Test
Password : 12345

=======Load Properties Type 3=======
Name : Test
Password : 12345

Leave a Reply

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