Mar 252013
 

As it says in the javadoc below, this class facilitates access to the resource properties. For the Java src “resources” package, this class will parse the “.properties” file of the name given in the constructor. There is definitely room for improvement in this, specifically in that the path is hard coded into the method “loadProperties”. In any case, modify it as needed.

package bolin;

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

/**
 * This class facilitates access to the resource properties.
 * @author Steven P. Bolin - Mar 25, 2013
 */
 public class PropertyReader {

 private String resourceName;
 private Properties prop;

/**
 * Constructor
 * @param resourceName - the name of the resource file to access. This file is
 * a file located in the resources source package.
 * Pre-requisite - resourceName != null and is a valid filename of a file located
 * in the resources source folder
 */
 public PropertyReader(String resourceName) {
   this.resourceName = resourceName;
   this.prop = new Properties();
   this.loadProperties();
 }

/**
 * Returns the property value for the property associated with propertyName or
 * null if no match is found
 * @param propertyName - The name (key) of the property to be returned.
 * @return Returns the property value for the property associated with propertyName or
 * null if no match is found
 */
 public String get(String propertyName) {
   String result = (propertyName == null) ? null : prop.getProperty(propertyName);
   return result;
 }

 // TODO: Update and correct the exception catch
 private void loadProperties() {
   InputStream in = this.getClass().getClassLoader().getResourceAsStream("resources/"+this.resourceName);
   try {
     this.prop.load(in);
   } catch (IOException e) {
     System.err.println("Failed to access "+this.resourceName+". ");
   }
 }
} // End of class

 

 

 Posted by at 12:00 am