How to read properties file in java

Properties files are used in java projects to externalise configuration, for example, database settings. In this post , we will see how to read properties file in java.

Java used Properties class to store above key values pair. Properties.load method is very convenient to load properties file in form of key values pairs.

Properties file looks something like this.



There are two ways you can do it.
  • Read properties file from system
  • Read properties file  from classpath

Read properties file from System :

In this, you need to read properties file from system path. Here I am putting properties file in root level of project.


java code:
package org.arpit.java2blog;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/*
 * @author Arpit Mandliya
 */
public class ReadPropertiesFileJavaMain {

 public static void main(String args[]) throws IOException
 {
  System.out.println("Reading from properties file");
  System.out.println("-----------------------------");
  Properties prop=readPropertiesFile("config.properties");
  
  System.out.println("host : "+prop.getProperty("host"));
  System.out.println("username : "+prop.getProperty("username"));
  System.out.println("password : "+prop.getProperty("password"));
  System.out.println("-----------------------------");
 }

 public static Properties readPropertiesFile(String fileName) throws IOException
 {
  FileInputStream fis=null;
  Properties prop=null;
  try {
   fis= new FileInputStream(fileName);
   // create Properties class object
   prop=new Properties();
   // load properties file into it
   prop.load(fis);

  } catch (FileNotFoundException e) {

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

   e.printStackTrace();
  }
  finally {
   fis.close();
  }

  return prop;
 }

}

When you run above program, you will get following output:
Reading from properties file
-----------------------------
host : localhost
username : java2blog
password : java123
-----------------------------

Read properties file from classpath: 

You can read properties file to classpath too. you have $project/src as default classpath as this src folder will be copied to classes. You can put it in $project/src folder and read it from there.


 you need to use
this.getClass().getResourceAsStream("/config.properties");
to read it from classpath.
package org.arpit.java2blog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/*
 * @author Arpit Mandliya
 */
public class ReadPropertiesFileJavaMain {

 public static void main(String args[]) throws IOException
 {
  ReadPropertiesFileJavaMain rp=new ReadPropertiesFileJavaMain();
  System.out.println("Reading from properties file");
  System.out.println("-----------------------------");
  
  Properties prop=rp.readPropertiesFile("/config.properties");

  System.out.println("host : "+prop.getProperty("host"));
  System.out.println("username : "+prop.getProperty("username"));
  System.out.println("password : "+prop.getProperty("password"));
  System.out.println("-----------------------------");
 }

 public  Properties readPropertiesFile(String fileName) throws IOException
 {
  InputStream fis=null;
  Properties prop=null;
  try {
   prop=new Properties();
   fis=this.getClass().getResourceAsStream(fileName);
  
   // create Properties class object
   if (fis != null) {
    // load properties file into it
    prop.load(fis);
   } else {
    throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
   }

  } catch (FileNotFoundException e) {

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

   e.printStackTrace();
  }
  finally {
   fis.close();
  }

  return prop;
 }

}

When you run above program, you will get following output:
Reading from properties file
-----------------------------
host : localhost
username : java2blog
password : java123
-----------------------------

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012