Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 - 2015, Neemworks Nigeria <dev@nimworks.com>
   Permission to use, copy, modify, and distribute this software for any
   purpose with or without fee is hereby granted, provided that the above
   copyright notice and this permission notice appear in all copies.
  
   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
 package nw.commons;
 
 import java.util.Set;
 
Provides a means of manipulating property files used by the project, automatically creates a property file if it does not see one in application root folder. Only a single instance of the class is expected to exists

Author(s):
Ogwara O. Rowland
Version:
0.3
Since:
10th November, 2013
 
 
 public class AppProperties {

The referenced properties file.
 
 	private Properties props = new Properties(); // Empty Java properties object
 
Optional comments attached to the property file.
 
 	private String comments = "Auto Generated Property File";

The property file name. Defaults to application.properties
 
 	private String fileName = "application.properties";

The logger.
 
 	private final Logger logger = LoggerFactory.getLogger(getClass());

Instantiates a new base properties, using the default property file name.
 
 	public AppProperties() {
 		this("application.properties");
 	}

Instantiates a new base properties.

Parameters:
fileName the file name
 
 	public AppProperties(String fileName) {
 		this. = fileName;
 	}

Load properties.

Returns:
the properties
 
 	private Properties loadProperties() {
 		try {
 			.load(fis);
 
 			fis.close();
 		} catch (FileNotFoundException e) {
 		} catch (IOException e) {
 		}
 		return ;
 	}

Creates a blank properties file.
 
 	private void createProperties() {
 		try {
			fos.flush();
			fos.close();
catch (Exception e) {
			.error("Exception while updating properties file: "e);
		}
	}

Parameters:
properties property map to add to property file
	public void saveProperties(HashMap<StringStringproperties) {
		Set<Stringkeys = properties.keySet();
		Iterator<Stringitr = keys.iterator();
		while (itr.hasNext()) {
			String key = itr.next();
			.setProperty(keyproperties.get(key));
		}
	}

sets the value for a property.

Parameters:
key reference key for property
value property value
	public synchronized void setProperty(String keyString value) {
		.setProperty(keyvalue);
	}

Retrieves a property with the specified key.

Parameters:
key target key to retrieve
defaultVal String value default
Returns:
defaultVal if key does not exist in the property file or returns the appropriate value
	public String getProperty(String keyString defaultVal) {
		return .getProperty(keydefaultVal);
	}

deletes an entry with the specified key.

Parameters:
key reference item to remove
	public synchronized void removeProperty(String key) {
	}

Gets the int.

Parameters:
key reference key
defaultVal value if property is not set
Returns:
retrieves int from property files
	public Integer getInt(String keyInteger defaultVal) {
		return Integer.valueOf(.getProperty(keydefaultVal + ""));
	}

Gets the long.

Parameters:
key refernce key
defaultVal value if property is not set
Returns:
retrieves Long from property files
	public Long getLong(String keyLong defaultVal) {
		return Long.valueOf(.getProperty(keydefaultVal + ""));
	}

Gets the big decimal.

Parameters:
key refernce key
defaultVal value if property is not set
Returns:
retrieves BigDecimal from property files
	public BigDecimal getBigDecimal(String keyBigDecimal defaultVal) {
		return new BigDecimal(.getProperty(keydefaultVal + ""));
	}

Gets the double.

Parameters:
key the key
defaultVal the default val
Returns:
retrieves double from property files
	public Double getDouble(String keyDouble defaultVal) {
		return Double.valueOf(.getProperty(keydefaultVal + ""));
	}

Gets the float.

Parameters:
key the key
defaultVal the default val
Returns:
retrieves an int from property files
	public Float getFloat(String keyFloat defaultVal) {
		return Float.valueOf(.getProperty(keydefaultVal + ""));
	}

Gets the boolean property for specified key.

Parameters:
key the property key
defaultVal the default value if nothing is found
Returns:
the bool value for the target key
	public Boolean getBool(String keyBoolean defaultVal){
		return Boolean.valueOf(.getProperty(keydefaultVal.toString()));
	}
New to GrepCode? Check out our FAQ X