Java - How to create and read properties file

By xngo on June 14, 2019

Java Properties is useful when you need to store simple parameters as key-value pairs. For example, INI configure file is a good fit.

Here is how to create and read properties file.

package net.openwritings.java.util.properties;
 
import java.util.Properties;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class MyProperties
{
    public static void main(String args[]){
        /**
        * Create and save the properties into a file.
        */
 
        // Create the properties object and add some values.
        Properties oWriteProperties = new Properties();
        oWriteProperties.setProperty("key1", "value1");
        oWriteProperties.setProperty("key2", "value2");
        oWriteProperties.setProperty("chinese", "中文");
 
        // Save properties into file. 
        try{
            oWriteProperties.store(new FileOutputStream("myproperties.properties"), 
                                                        "my first properties list");
        }
        catch(FileNotFoundException ex){
            System.out.println(ex.getMessage());
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
 
        /**
        * Read properties file created.
        */
        Properties oReadProperties = new Properties();
        try{
            oReadProperties.load(new FileInputStream("myproperties.properties"));
        }
        catch(FileNotFoundException ex){
            System.out.println(ex.getMessage());
        }
        catch(IOException ex){
            System.out.println(ex.getMessage());
        }
 
        System.out.println(oReadProperties.getProperty("key2"));
        System.out.println(oReadProperties.getProperty("chinese"));
    }
}

Output

Output of Java property file

Github

  • https://github.com/xuanngo2001/java-small/blob/master/src/net/openwritings/java/util/properties/MyProperties.java

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.