Class PropertiesFile

java.lang.Object
  extended by PropertiesFile

public final class PropertiesFile
extends java.lang.Object

Used for accessing and creating .[properties] files, reads them as utf-8, saves as utf-8. Internationalization is key importance especially for character codes.


Constructor Summary
Constructor and Description
PropertiesFile(java.lang.String fileName)
          Creates or opens a properties file using specified filename
 
Method Summary
Modifier and Type Method and Description
 boolean containsKey(java.lang.String var)
          Checks to see if the .[properties] file contains the given key.
 boolean getBoolean(java.lang.String key)
          Returns the value of the key given in a Boolean, however we do not set a string if no key is found.
 boolean getBoolean(java.lang.String key, boolean value)
          Returns the boolean value of a key
 double getDouble(java.lang.String key)
          Returns the value of the key given in a Double, however we do not set a string if no key is found.
 double getDouble(java.lang.String key, double value)
          Returns the double value of a key
 int getInt(java.lang.String key)
          Returns the value of the key given in a Integer, however we do not set a string if no key is found.
 int getInt(java.lang.String key, int value)
          Returns the int value of a key
 long getLong(java.lang.String key)
          Returns the value of the key given in a Long, however we do not set a string if no key is found.
 long getLong(java.lang.String key, long value)
          Returns the long value of a key
 java.lang.String getProperty(java.lang.String var)
          Checks to see if this key exists in the .[properties] file.
 java.lang.String getString(java.lang.String key)
          Returns the value of the key given as a String, however we do not set a string if no key is found.
 java.lang.String getString(java.lang.String key, java.lang.String value)
          Returns the value of the key given as a String.
 boolean keyExists(java.lang.String key)
          Checks the existance of a key.
 void load()
          The loader for property files, it reads the file as UTF8 or converts the string into UTF8.
 void removeKey(java.lang.String var)
          Remove a key from the file if it exists.
 java.util.Map<java.lang.String,java.lang.String> returnMap()
          Returns a Map of all key=value properties in the file as <key (java.lang.String), value (java.lang.String)>

Example:
 void save()
          Writes out the key=value properties that were changed into a .[properties] file in UTF8.
 void setBoolean(java.lang.String key, boolean value)
          Save the value given as a boolean on the specified key.
 void setDouble(java.lang.String key, double value)
          Save the value given as a double on the specified key.
 void setInt(java.lang.String key, int value)
          Save the value given as a int on the specified key.
 void setLong(java.lang.String key, long value)
          Save the value given as a long on the specified key.
 void setString(java.lang.String key, java.lang.String value)
          Save the value given as a String on the specified key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertiesFile

public PropertiesFile(java.lang.String fileName)
Creates or opens a properties file using specified filename

Parameters:
fileName -
Method Detail

load

public void load()
          throws java.io.IOException
The loader for property files, it reads the file as UTF8 or converts the string into UTF8. Used for simple runthrough's, loading, or reloading of the file.

Throws:
java.io.IOException

save

public void save()
Writes out the key=value properties that were changed into a .[properties] file in UTF8.


returnMap

public java.util.Map<java.lang.String,java.lang.String> returnMap()
                                                           throws java.lang.Exception
Returns a Map of all key=value properties in the file as <key (java.lang.String), value (java.lang.String)>

Example:
 PropertiesFile settings = new PropertiesFile("settings.properties");
 Map mappedSettings;
 
 try {
         mappedSettings = settings.returnMap();
 } catch (Exception ex) {
         log.info("Failed mapping settings.properties");
 }
 

Returns:
map - Simple Map HashMap of the entire key=value as <key (java.lang.String), value (java.lang.String)>
Throws:
java.lang.Exception - If the properties file doesn't exist.

containsKey

public boolean containsKey(java.lang.String var)
Checks to see if the .[properties] file contains the given key.

Parameters:
var - The key we are going to be checking the existance of.
Returns:
Boolean - True if the key exists, false if it cannot be found.

getProperty

public java.lang.String getProperty(java.lang.String var)
Checks to see if this key exists in the .[properties] file.

Parameters:
var - The key we are grabbing the value of.
Returns:
java.lang.String - True if the key exists, false if it cannot be found.

removeKey

public void removeKey(java.lang.String var)
Remove a key from the file if it exists. This will save() which will invoke a load() on the file.

Parameters:
var - The key that will be removed from the file
See Also:
save()

keyExists

public boolean keyExists(java.lang.String key)
Checks the existance of a key.

Parameters:
key - The key in question of existance.
Returns:
Boolean - True for existance, false for key found.
See Also:
containsKey(java.lang.String)

getString

public java.lang.String getString(java.lang.String key)
Returns the value of the key given as a String, however we do not set a string if no key is found.

Parameters:
key - The key we will retrieve the property from, if no key is found default to "" or empty.
See Also:
getProperty(java.lang.String)

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String value)
Returns the value of the key given as a String. If it is not found, it will invoke saving the default value to the properties file.

Parameters:
key - The key that we will be grabbing the value from, if no value is found set and return value
value - The default value that we will be setting if no prior key is found.
Returns:
java.lang.String Either we will return the default value or a prior existing value depending on existance.
See Also:
setString(java.lang.String, java.lang.String), getProperty(java.lang.String)

setString

public void setString(java.lang.String key,
                      java.lang.String value)
Save the value given as a String on the specified key.

Parameters:
key - The key that we will be addressing the value to.
value - The value we will be setting inside the .[properties] file.
See Also:
save()

getInt

public int getInt(java.lang.String key)
Returns the value of the key given in a Integer, however we do not set a string if no key is found.

Parameters:
key - The key we will retrieve the property from, if no key is found default to 0
See Also:
getProperty(String var)

getInt

public int getInt(java.lang.String key,
                  int value)
Returns the int value of a key

Parameters:
key - The key that we will be grabbing the value from, if no value is found set and return value
value - The default value that we will be setting if no prior key is found.
Returns:
Integer - Either we will return the default value or a prior existing value depending on existance.
See Also:
setInt(String key, int value)

setInt

public void setInt(java.lang.String key,
                   int value)
Save the value given as a int on the specified key.

Parameters:
key - The key that we will be addressing the value to.
value - The value we will be setting inside the .[properties] file.
See Also:
save()

getDouble

public double getDouble(java.lang.String key)
Returns the value of the key given in a Double, however we do not set a string if no key is found.

Parameters:
key - The key we will retrieve the property from, if no key is found default to 0.0
See Also:
getProperty(String var)

getDouble

public double getDouble(java.lang.String key,
                        double value)
Returns the double value of a key

Parameters:
key - The key that we will be grabbing the value from, if no value is found set and return value
value - The default value that we will be setting if no prior key is found.
Returns:
Double - Either we will return the default value or a prior existing value depending on existance.
See Also:
setDouble(String key, double value)

setDouble

public void setDouble(java.lang.String key,
                      double value)
Save the value given as a double on the specified key.

Parameters:
key - The key that we will be addressing the value to.
value - The value we will be setting inside the .[properties] file.
See Also:
save()

getLong

public long getLong(java.lang.String key)
Returns the value of the key given in a Long, however we do not set a string if no key is found.

Parameters:
key - The key we will retrieve the property from, if no key is found default to 0L
See Also:
getProperty(String var)

getLong

public long getLong(java.lang.String key,
                    long value)
Returns the long value of a key

Parameters:
key - The key that we will be grabbing the value from, if no value is found set and return value
value - The default value that we will be setting if no prior key is found.
Returns:
Long - Either we will return the default value or a prior existing value depending on existance.
See Also:
setLong(String key, long value)

setLong

public void setLong(java.lang.String key,
                    long value)
Save the value given as a long on the specified key.

Parameters:
key - The key that we will be addressing the value to.
value - The value we will be setting inside the .[properties] file.
See Also:
save()

getBoolean

public boolean getBoolean(java.lang.String key)
Returns the value of the key given in a Boolean, however we do not set a string if no key is found.

Parameters:
key - The key we will retrieve the property from, if no key is found default to false
See Also:
getProperty(String var)

getBoolean

public boolean getBoolean(java.lang.String key,
                          boolean value)
Returns the boolean value of a key

Parameters:
key - The key that we will be grabbing the value from, if no value is found set and return value
value - The default value that we will be setting if no prior key is found.
Returns:
Boolean - Either we will return the default value or a prior existing value depending on existance.
See Also:
setBoolean(String key, boolean value)

setBoolean

public void setBoolean(java.lang.String key,
                       boolean value)
Save the value given as a boolean on the specified key.

Parameters:
key - The key that we will be addressing the value to.
value - The value we will be setting inside the .[properties] file.
See Also:
save()