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“.

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

Output

config.properties
#Mon Jan 11 18:54:40 MYT 2010
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.

package com.mkyong.common;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
 
public class App 
{
    public static void main( String[] args )
    {
    	Properties prop = new Properties();
 
    	try {
               //load a properties file
    		prop.load(new FileInputStream("config.properties"));
 
               //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();
        }
 
    }
}

Output

localhost
mkyong
password

3. Load a properties file from classpath

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

package com.mkyong.common;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.utilutil.Properties;
 
public class App 
{
    public static void main( String[] args )
    {
    	Properties prop = new Properties();
 
    	try {
               //load a properties file from class path, inside static method
    		prop.load(App.class.getClassLoader().getResourceAsStream("config.properties");));
 
               //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();
        }
 
    }
}

For non-static method, use this :

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

References

  1. Java : GetResourceAsStream In Static Method
  2. Java Properties JavaDoc
Tags :
Founder of Mkyong.com, love Java and open source stuffs. Follow him on Twitter, or befriend him on Facebook or Google Plus.
Here are some of my recommended Books

Related Posts

Popular Posts