Java Properties file examples

Normally, Java properties file is used to store project configuration data or settings. In this tutorial, we will show you how to read and write to/from a properties file.

1. Write to properties file

Set the property value, and write it into a properties file named config.properties.

App.java
package com.mkyong.properties;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
 
public class App {
  public static void main(String[] args) {
 
	Properties prop = new Properties();
	OutputStream output = null;
 
	try {
 
		output = new FileOutputStream("config.properties");
 
		// set the properties value
		prop.setProperty("database", "localhost");
		prop.setProperty("dbuser", "mkyong");
		prop.setProperty("dbpassword", "password");
 
		// save properties to project root folder
		prop.store(output, null);
 
	} catch (IOException io) {
		io.printStackTrace();
	} finally {
		if (output != null) {
			try {
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
 
	}
  }
}

Output

config.properties
#Fri Jan 17 22:37:45 MYT 2014
dbpassword=password
database=localhost
dbuser=mkyong

P.S If file path is not specified, then this properties file will be stored in your project root folder.

2. Load a properties file

Load a properties file from the file system and retrieved the property value.

App.java
package com.mkyong.properties;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class App {
  public static void main(String[] args) {
 
	Properties prop = new Properties();
	InputStream input = null;
 
	try {
 
		input = new FileInputStream("config.properties");
 
		// load a properties file
		prop.load(input);
 
		// get the property value and print it out
		System.out.println(prop.getProperty("database"));
		System.out.println(prop.getProperty("dbuser"));
		System.out.println(prop.getProperty("dbpassword"));
 
	} catch (IOException ex) {
		ex.printStackTrace();
	} finally {
		if (input != null) {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
  }
}

Output

localhost
mkyong
password

3. Load a properties file from classpath

Load a properties file config.properties from project classpath, and retrieved the property value.

P.S Assume properties file “config.properties” is in your project classpath root folder.

App.java
package com.mkyong.properties;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class App {
 
  public static void main( String[] args ){
 
    	Properties prop = new Properties();
    	InputStream input = null;
 
    	try {
 
    		String filename = "config.properties";
    		input = App3.class.getClassLoader().getResourceAsStream(filename);
    		if(input==null){
    	            System.out.println("Sorry, unable to find " + filename);
    		    return;
    		}
 
    		//load a properties file from class path, inside static method
    		prop.load(input);
 
                //get the property value and print it out
                System.out.println(prop.getProperty("database"));
    	        System.out.println(prop.getProperty("dbuser"));
    	        System.out.println(prop.getProperty("dbpassword"));
 
    	} catch (IOException ex) {
    		ex.printStackTrace();
        } finally{
        	if(input!=null){
        		try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        	}
        }
 
    }
}

Output

localhost
mkyong
password

For non-static method, use this :

prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));

4. Prints everything from a properties file

Load a properties file config.properties from project classpath, and get the keys and values.

App.java
package com.mkyong.properties;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
 
public class App {
 
  public static void main(String[] args) {
	App app = new App();
	app.printThemAll();
  }
 
  private void printThemAll() {
 
	Properties prop = new Properties();
	InputStream input = null;
 
	try {
 
		String filename = "config.properties";
		input = getClass().getClassLoader().getResourceAsStream(filename);
		if (input == null) {
			System.out.println("Sorry, unable to find " + filename);
			return;
		}
 
		prop.load(input);
 
		Enumeration<?> e = prop.propertyNames();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			String value = prop.getProperty(key);
			System.out.println("Key : " + key + ", Value : " + value);
		}
 
	} catch (IOException ex) {
		ex.printStackTrace();
	} finally {
		if (input != null) {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
  }
 
}

Output

Key : dbpassword, Value : password
Key : database, Value : localhost
Key : dbuser, Value : mkyong

References

  1. Java : GetResourceAsStream In Static Method
  2. Java Properties JavaDoc
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • http://ask.fm/idziduatilu Faridzi

    always there to help one in need :) thank mkyong

  • https://plus.google.com/105282634276994171010 Ankit Gupta

    getting NullPointer exception in the 3rd scenario:-

    Exception in thread “main” java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at com.properties.reader.AppReaderCpath.main(AppReaderCpath.java:15)

  • Yatin

    Thanks a lot..
    Your blog is in my help list.. :)

  • Ruly

    Hi,
    Thanks for the tutorial, it works for me when create servlet and deployed in tomcat. But I got NullPointerException in weblogic, The path for the properties file is in WEB-INF folder. Maybe you know why it failed?

    regards,
    Ruly

  • Alvaro

    Clear and easy like Rafael said. Thanks a lot.

  • Mordechai Serraf

    Your tutorials are always so good. Short sweet and to the point, thanks man.

    • Alex Costa

      My thoughts exactly

  • Rafael

    Thanks MkYoung, explanation clear and easy .

  • Vijairam

    This was so lucid and concise.
    You just saved half hour of my life by writing this article.

    May the force be with you!

    Best!
    Vijai