How to read properties file using JAVA


First create a properties file like below; And put it the package folder where you can read and access this file

config.properties

# config.properties
# author: tuna tore
!this is a comment
#this is a comment too
! \ means a multiline is exist

keyForProperty1=Hello
keyForProperty2=Hello World!!!
123=an integer property is OK!

keyForProperty3= the value of the property3 \
                 new line \
                 new line2

And code a Main.java file like below;

package readingpropertiesfileexample;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

/**
 *
 * @author tunatore
 */
public class Main {

    public static void main(String[] args) {

     try {          
 
            InputStream inputStream = Main.class.getResource(“config.properties“).openStream();
            Properties properties = new Properties();
            properties.load(inputStream);
               
            String property1 = properties.getProperty(“keyForProperty1“);
            String property2 = properties.getProperty(“keyForProperty2“);           
            String property3 = properties.getProperty(“keyForProperty3“);
            String property4 = properties.getProperty(“123“);

            System.out.println(“property1: ” + property1);
            System.out.println(“property2: ” + property2);
            System.out.println(“multiline property3: ” + property3);
            System.out.println(“integer property4: ” + property4);

            System.out.println(“#######Iteration over keyset#######STARTS…..”);

            for (Iterator it = properties.keySet().iterator(); it.hasNext();) {
                    String propertyKey = (String) it.next();
                    System.out.println(“Iteration key: ” + propertyKey + ” key value: ”
                            + properties.getProperty(propertyKey));
            }
            System.out.println(“#######Iteration over keyset#######ENDS…….”);

            inputStream.close();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());

        }
    }
}

After running the output is;

run:
property1: Hello
property2: Hello World!!!
multiline property3: the value of the property3 new line new line2
integer property4: an integer property is OK!
#######Iteration over keyset#######STARTS…..
Iteration key: 123 key value: an integer property is OK!
Iteration key: keyForProperty3 key value: the value of the property3 new line new line2
Iteration key: keyForProperty2 key value: Hello World!!!
Iteration key: keyForProperty1 key value: Hello
#######Iteration over keyset#######ENDS…….
BUILD SUCCESSFUL (total time: 0 seconds)

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s