skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

Generic to Load Properties file

Posted by Admin at 8:33 AM – 0 comments
 

This Generic loads a properties file to the application and reads the values from the properties file.



import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;


public class PropertiesValueLoader {
 
 private static Properties defaults = new Properties();
 InputStream in= null;

 public void loadVariable(String propertyFile)throws IOException {
  in=this.getClass().getClassLoader() .getResourceAsStream(propertyFile);
  try{
   if (in != null) {
    defaults.load (in); 
   }
   } 
  catch (IOException e) {
   defaults = null;
  }
  finally {
   if (in != null) try { in.close (); } catch (IOException e) {}
  }
 }
  
 public String getValue(String field) {
  String value = defaults.getProperty(field);
  if (value == null) {
   return null;
  }
  return value.trim();
 }
     
 public int getValueInt(String field) {
  String value = defaults.getProperty(field);
  int intValue=0;
  if (value == null) {
   return intValue;
  }
  intValue=Integer.parseInt(value.trim());
  return intValue;
 }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger