How To Load Property Files Smartly
What is the best strategy for loading property and configuration files in Java?
1.Absolute path for property files.
2.loading resources through classloaders.
3.Load file using Resourse bundle.
1>Absolute path for Property file
//property.properties
register.mail.file : usermail.html
invite.game.friend.mail.file : invitegamefriend.html
scrap.game.friend.mail.file : scrap.html
Public void loadpropertyfile{
Properties propertirs = new Properties();
propertirs.load(”/home/manu/property.properties”);
propertirs.list(System.out);
System.out.println("\nThe foo property: " + propertirs.getProperty("food"));
}
U can Load xml property file also
//sample.properties file (xml file)
Hi
struts
manu
Public void loadpropertyfileForXml{
Properties propertirs = new Properties();
propertirs.loadFromXml(”/home/manu/sample,properties”);
propertirs.list(System.out);
System.out.println("\nThe foo property:xml " + propertirs.getProperty("food"));
}
Loading Through class LoaderThis is one of the best methord to load property file
To load property file using class laoder the file must be put under the
class path(where the classes are resides).
1.ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
2.Class.getResourceAsStream ("/some/pkg/resource.properties");
3.getClass().getClassLoader().getSystemResourceAsStream(”/some/pkg/resource.properties”);
public class LoadSampleXml
{
public void initialize() throws InvalidPropertiesFormatException, IOException
{
//Properties propertirs=null;
Properties propertirs = new Properties();
// propertirs.loadFromXML(this.getClass().getResourceAsStream("samplexml.properties"));
// propertirs.loadFromXML(getClass().getClassLoader().getResourceAsStream("samplexml.properties"));
// propertirs.loadFromXML(getClass().getClassLoader().getSystemResourceAsStream("samplexml.properties"));
// propertirs.load(getClass().getClassLoader().getResourceAsStream("emailProperties.properties"));
propertirs.loadFromXML(getClass().getClassLoader().getResourceAsStream("res/samplexml.properties"));
propertirs.list(System.out);
System.out.println("\nThe java property:xml " + propertirs.getProperty("java"));
}
}
2>Using resource bundle
It must use when u requires locale specific property file. resource bundle is class u can see it anywhere in sun java site.i m attaching a souce code to use resource bundle as to load property file.here also property file must be there in classpath.
getBundle is methord of ResourceBundle Class.
When the getBundle method locates the correct properties file, it returns a PropertyResourceBundle object containing the key-value pairs from the properties file.
code is here
public class PropertyTest {
public void initialize()
{
System.out.println(getClass().getClassLoader()+"dfsasdfasfasdfdasfasfas");
Properties config=loadProperties("emailProperties", getClass().getClassLoader());
System.out.println(config);
}
public Properties loadProperties(String name, ClassLoader loader)
{
System.out.println(ClassLoader.getSystemClassLoader().getResourceAsStream("samplexml.properties")+"system class loader");
if (name == null)
throw new IllegalArgumentException("Property file name : null");
Properties result = null;
try
{
if (loader == null)
loader = ClassLoader.getSystemClassLoader();
// Throws MissingResourceException on lookup failures:
final ResourceBundle rb = ResourceBundle.getBundle(name, Locale.ENGLISH,loader);
result = new Properties();
for(Enumeration keys = rb.getKeys(); keys.hasMoreElements();)
{
final String key = (String) keys.nextElement();
final String value = rb.getString(key);
result.put(key, value);
}
}
catch (Exception e)
{
result = null;
}
if (result == null)
{
throw new IllegalArgumentException("Could not load [" + name + "]" + " as a resource bundle");
}
return result;
}
}
Post Options
What is the best strategy for loading property and configuration files in Java?
1.Absolute path for property files.
2.loading resources through classloaders.
3.Load file using Resourse bundle.
1>Absolute path for Property file
//property.properties
register.mail.file : usermail.html
invite.game.friend.mail.file : invitegamefriend.html
scrap.game.friend.mail.file : scrap.html
Public void loadpropertyfile{
Properties propertirs = new Properties();
propertirs.load(”/home/manu/property.properties”);
propertirs.list(System.out);
System.out.println("\nThe foo property: " + propertirs.getProperty("food"));
}
U can Load xml property file also
//sample.properties file (xml file)
Public void loadpropertyfileForXml{
Properties propertirs = new Properties();
propertirs.loadFromXml(”/home/manu/sample,properties”);
propertirs.list(System.out);
System.out.println("\nThe foo property:xml " + propertirs.getProperty("food"));
}
Loading Through class LoaderThis is one of the best methord to load property file
To load property file using class laoder the file must be put under the
class path(where the classes are resides).
1.ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
2.Class.getResourceAsStream ("/some/pkg/resource.properties");
3.getClass().getClassLoader().getSystemResourceAsStream(”/some/pkg/resource.properties”);
public class LoadSampleXml
{
public void initialize() throws InvalidPropertiesFormatException, IOException
{
//Properties propertirs=null;
Properties propertirs = new Properties();
// propertirs.loadFromXML(this.getClass().getResourceAsStream("samplexml.properties"));
// propertirs.loadFromXML(getClass().getClassLoader().getResourceAsStream("samplexml.properties"));
// propertirs.loadFromXML(getClass().getClassLoader().getSystemResourceAsStream("samplexml.properties"));
// propertirs.load(getClass().getClassLoader().getResourceAsStream("emailProperties.properties"));
propertirs.loadFromXML(getClass().getClassLoader().getResourceAsStream("res/samplexml.properties"));
propertirs.list(System.out);
System.out.println("\nThe java property:xml " + propertirs.getProperty("java"));
}
}
2>Using resource bundle
It must use when u requires locale specific property file. resource bundle is class u can see it anywhere in sun java site.i m attaching a souce code to use resource bundle as to load property file.here also property file must be there in classpath.
getBundle is methord of ResourceBundle Class.
When the getBundle method locates the correct properties file, it returns a PropertyResourceBundle object containing the key-value pairs from the properties file.
code is here
public class PropertyTest {
public void initialize()
{
System.out.println(getClass().getClassLoader()+"dfsasdfasfasdfdasfasfas");
Properties config=loadProperties("emailProperties", getClass().getClassLoader());
System.out.println(config);
}
public Properties loadProperties(String name, ClassLoader loader)
{
System.out.println(ClassLoader.getSystemClassLoader().getResourceAsStream("samplexml.properties")+"system class loader");
if (name == null)
throw new IllegalArgumentException("Property file name : null");
Properties result = null;
try
{
if (loader == null)
loader = ClassLoader.getSystemClassLoader();
// Throws MissingResourceException on lookup failures:
final ResourceBundle rb = ResourceBundle.getBundle(name, Locale.ENGLISH,loader);
result = new Properties();
for(Enumeration keys = rb.getKeys(); keys.hasMoreElements();)
{
final String key = (String) keys.nextElement();
final String value = rb.getString(key);
result.put(key, value);
}
}
catch (Exception e)
{
result = null;
}
if (result == null)
{
throw new IllegalArgumentException("Could not load [" + name + "]" + " as a resource bundle");
}
return result;
}
}
Post Options
No comments:
Post a Comment