Java Properties file Read and Write example - .properties and .xml

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.


Properties are configuration values managed as key/value pairs. In each pair, the key and value are both String values. The key identifies, and is used to retrieve, the value, much as a variable name is used to retrieve the variable's value. Each line in a .properties file normally stores a single property. Several formats are possible for each line, including key=value, key = value, key:value, and key value.
.properties files can use the number sign (#) or the exclamation mark (!) as the first non blank character in a line to denote that all text following it is a comment. The backwards slash is used to escape a character.

An example of a properties file is provided below.

# You are reading the ".properties" file.
! The exclamation mark can also mark text as comments.
website = http://as400samplecode.blogspot.com/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
          iSeries Programmers Sample Guide
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Unicode
tab : \u0009

Sample Java Code to write .properties file

package com.as400samplecode;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SaveJDBCInfo {
   
    public static void main( String[] args )
    {
        Properties prop = new Properties();

        try {
            //set the properties value
            prop.setProperty("host_name", "localhost");
            prop.setProperty("user_id", "as400");
            prop.setProperty("bpassword", "samplecode");

            //save properties to project root folder
            prop.store(new FileOutputStream("myjdbc.properties"), null);

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

}

Output for myjdbc.properties file

#Sun May 22 23:58:00 EDT 2011
host_name=localhost
user_id=as400
password=samplecode

Sample Java program to read .properties file

package com.as400samplecode;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ReadJDBCInfo {

    public static void main( String[] args )
    {
        Properties prop = new Properties();

        try {
            //load a properties file
            prop.load(new FileInputStream("myjdbc.properties"));

            //get the property value and print it out
            System.out.println(prop.getProperty("host_name"));
            System.out.println(prop.getProperty("user_id"));
            System.out.println(prop.getProperty("password"));

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

    }

}

Output for above

localhost
as400
samplecode

Java program to store properties in an XML file

package com.as400samplecode;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class WriteXMLPropertiesFile {

    public static void main(String[] args) {

        Properties prop = new Properties();

        try {
          
            //set the properties value
            prop.setProperty("hostname", "http://as400samplecode.blogspot.com");
            prop.setProperty("userId", "as400samplecode");
            prop.setProperty("password", "google");

            //store the properties detail into a XML file
            FileOutputStream outputStream = new FileOutputStream("properties/jdbcConfig.xml");
            prop.storeToXML(outputStream, "My JDBC Configuration file","UTF-8");
          
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

XML Properties file created using the above program - jdbcConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>My JDBC Configuration file</comment>
<entry key="hostname">http://as400samplecode.blogspot.com</entry>
<entry key="password">google</entry>
<entry key="userId">as400samplecode</entry>
</properties>

Java program to read properties from the XML file created above

package com.as400samplecode;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ReadXMLPropertiesFile {
  
        public static void main(String[] args) {
  
            Properties prop = new Properties();

            try {
                //load a properties file
                prop.loadFromXML(new FileInputStream("properties/jdbcConfig.xml"));

                //get the property value and print it out
                System.out.println(prop.getProperty("hostname"));
                System.out.println(prop.getProperty("userId"));
                System.out.println(prop.getProperty("password"));

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

}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.