Java Properties file example

  • 07 December 2016
  • ADM

 

Java Properties file example - images/logos/java.jpg

 

Properties files are used usually to store configurations data. For example log4j was storing the configurations into a properties file, until version 2 was release.

Examples

Here is a few manipulation for properties files. A properties file is a key value file.

Properties file example db.config

#Wed Dec 07 14:24:25 GMT 2016
db.host=localhost
db.port=3306
db.user=root
db.pass=password

Note: first line the timestamp when the file was created.

Write to properties file

   /**
     * 
     * write {@link Properties} object to a file 
     *
     * @param prop - {@link Properties} object
     * @param filePath - file location
     */
    public static void writeProperties(Properties prop, String filePath) {
	OutputStream output = null;

	try {

	    output = new FileOutputStream(filePath);

	    /**
	     * save the properties file to the location provided as parameter
	     */
	    prop.store(output, null);

	} catch (IOException io) {
	    /** Just print the exception */
	    io.printStackTrace();
	} finally {
	    /** Close the OutputStream object */
	    if (output != null) {
		try {
		    output.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }

	}
    }

Load a properties file

    /**
     * 
     * Load a file into {@link Properties} file
     *
     * @param filePath - file location
     * @return {@link Properties} object
     */
    public static Properties loadFile(String filePath) {
	Properties prop = new Properties();
	InputStream input = null;
	try {
	    input = new FileInputStream(filePath);
	    /** load the properties file */
	    prop.load(input);
	} catch (IOException ex) {
	    /** Just print the exception */
	    ex.printStackTrace();
	} finally {
	    /** Close the InputStream object */
	    if (input != null) {
		try {
		    input.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
	/** return the properties object */
	return prop;
    }

Usage

package com.admfactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesExample {

    /**
     * 
     * Load a file into {@link Properties} file
     *
     * @param filePath - file location
     * @return {@link Properties} object
     */
    public static Properties loadFile(String filePath) {
	Properties prop = new Properties();
	InputStream input = null;
	try {
	    input = new FileInputStream(filePath);
	    /** load the properties file */
	    prop.load(input);
	} catch (IOException ex) {
	    /** Just print the exception */
	    ex.printStackTrace();
	} finally {
	    /** Close the InputStream object */
	    if (input != null) {
		try {
		    input.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
	/** return the properties object */
	return prop;
    }

    /**
     * 
     * write {@link Properties} object to a file 
     *
     * @param prop - {@link Properties} object
     * @param filePath - file location
     */
    public static void writeProperties(Properties prop, String filePath) {
	OutputStream output = null;

	try {

	    output = new FileOutputStream(filePath);

	    /**
	     * save the properties file to the location provided as parameter
	     */
	    prop.store(output, null);

	} catch (IOException io) {
	    /** Just print the exception */
	    io.printStackTrace();
	} finally {
	    /** Close the OutputStream object */
	    if (output != null) {
		try {
		    output.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }

	}
    }

    public static void main(String[] args) {

	Properties propW = new Properties();
	propW.put("db.host", "localhost");
	propW.put("db.port", "3306");
	propW.put("db.user", "root");
	propW.put("db.pass", "password");

	System.out.println("Properties File Example");
	System.out.println();
	System.out.println("Properties File: " + propW);

	System.out.println("Write properties to file");
	writeProperties(propW, "db.config");

	System.out.println("Read properties file");
	Properties propR = loadFile("db.config");
	System.out.println("Properties File: " + propR);
    }

}

Output

Console

Properties File Example

Properties File: {db.user=root, db.port=3306, db.pass=password, db.host=localhost}
Write properties to file
Read properties file
Properties File: {db.port=3306, db.user=root, db.host=localhost, db.pass=password}

File

#Wed Dec 07 14:24:25 GMT 2016
db.user=root
db.port=3306
db.pass=password
db.host=localhost

Note: If file path is not specified, the properties file will be created in the project root folder.

 

References