Objective
Reading properties from "application.properties" into java.util.Properties.Properties object. This object can be used statically across project. Reading properties file will be single time activity in the life cycle of application developed.
Java Code :
package test.simplejavaimport java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
class APPLICATION_PROPERTIES {
static Properties staticProperties = new Properties();
static{
InputStream input = null;
try
{
input = new FileInputStream(" source file path ");
staticProperties.load(input);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
APPLICATION_PROPERTIES will be used in other classes as follows :
package test.simplejava
import java.util;
import static test.simplejava.APPLICATION_PROPERTIES.staticProperties ;
class testProperties
{
public static void main(String [] args)
{
Enumeration e = staticProperties.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key + " -- " + staticProperties.getProperty(key));
}
}
}
No comments:
Post a Comment