-- Read a Property File --

--------------------- ERA.properties --------------------
webapp.name=ERA Admin DashBoard
webapp.version=Version: neo_jsf_ice_with_tree_06

author.name=Jayasanka Illangarathne
author.url=http://jayasanka.blogspot.com

company.name=Virtusa Private Ltd:
company.url=http://www.virtusa.com
copyright.year=2008

dataBase.name=EraDB
server.path=jdbc:sqlserver://localhost:1433
dataBase.uname=sa
dataBase.passwd=jayasanka

-------------------------java file-------------------
package com.resource;

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

public class ERAProperties {

    public static void main(String[] args) {

        Properties properties = new Properties();

        try {
            // load the resource file
            properties.load(new FileInputStream("src/com/resource/ERA.properties"));
            // get a property
            String name = properties.getProperty("author.name");
            System.out.println(name);
            // set a property
            properties.setProperty("b'day", "04-02-1984");

            // Write properties file.
            properties.store(new FileOutputStream("src/com/resource /ERA.properties"), null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

NOTE: if you are using web-base project, put these property files in to /WEB-INF/classes/ folder and replace property.load(....) statement with following.
properties.load(Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("ERA.properties"));

Comments

Popular posts from this blog

-- How to Schedule a Task --

How to Connect DataBase to Java Application -JDBC

-- Are you a Android Developer? --