Thursday, June 9, 2011

Sample Java Program to access Properties file


# ServicesInfo.properties
#==================
#

Service.1 = http://intranet/contact/services/Infojsp.jsp?ServiceId=1&LogonUser=
Service.2 = http://intranet/contact/services/Infojsp.jsp?ServiceId=2&EID=
Service.3 = http://intranet/contact/services/Infojsp.jsp?ServiceId=4&EID=
Service.4 = http://intranet/contact/services/Infojsp.jsp?ServiceId=11&EID=
Service.5 = http://intranet/contact/services/Infojsp.jsp?ServiceId=10&Group=HRD
Service.6 = http://intranet/contact/services/Infojsp.jsp?ServiceID=13
Service.7 = http://intranet/contact/services/Infojsp.jsp?ServiceId=14&EID=

// Sample Java Program to access Properties file


import java.util.ResourceBundle;
import java.util.PropertyResourceBundle;

public class Sample
{
public static final String SERVICES_INFO_PROP_FILE = "ServicesInfo";

public static String getMessage(String service)
{
PropertyResourceBundle pFile = null;
String serviceUrl = null;
   
try
    {
    pFile = (PropertyResourceBundle) ResourceBundle.getBundle(SERVICES_INFO_PROP_FILE);
serviceUrl = (String)pFile.handleGetObject(service);
    }
    catch (Exception e)
    { }

      return serviceUrl;
}

public static void main(String[] args)
{
System.out.println("Message : " + getMessage("Service.5"));
}
}