com.huguesjohnson
Class IniFile

java.lang.Object
  extended by com.huguesjohnson.IniFile

public class IniFile
extends java.lang.Object

Reads/writes old-school Windows style .ini files.

Recommended usage:

        private void readSavedSettings(){
IniFile iniFile=new IniFile(this.settingsPath+File.separator+"YourAppName.ini");
try{
iniFile.read();
} catch(IOException x){
//something bad happened
}
final String defaultString=new String("YourDefaultValue");
String property1=new String(iniFile.getProperty("YourAppProperty",defaultString));
String property2=new String(iniFile.getProperty("AnotherAppProperty",defaultString));
//and so on..
}
}
private void saveSettings(){
IniFile iniFile=new IniFile(this.settingsPath+File.separator+"YourAppName.ini");
if(iniFile!=null){
iniFile.setProperty("YourAppProperty",this.someVariable);
iniFile.setProperty("AnotherAppProperty",this.anotherVariable);
iniFile.save("some header");
}
}

Author:
Hugues Johnson

Constructor Summary
IniFile(java.lang.String fileName)
          Create a new instance of IniFile.
 
Method Summary
 void clear()
          Clears out all loaded properties, does not modify the underlying file.
 boolean getProperty(java.lang.String name, boolean defaultValue)
          Returns a property from memory.
 byte getProperty(java.lang.String name, byte defaultValue)
          Returns a property from memory.
 char getProperty(java.lang.String name, char defaultValue)
          Returns a property from memory.
 double getProperty(java.lang.String name, double defaultValue)
          Returns a property from memory.
 float getProperty(java.lang.String name, float defaultValue)
          Returns a property from memory.
 int getProperty(java.lang.String name, int defaultValue)
          Returns a property from memory.
 long getProperty(java.lang.String name, long defaultValue)
          Returns a property from memory.
 java.lang.String getProperty(java.lang.String name, java.lang.String defaultValue)
          Returns a property from memory.
 void read()
          Reads the file specified in the constuctor.
 void save(java.lang.String header)
          Saves settings to file specified in the constuctor.
 void setProperty(java.lang.String name, boolean value)
          Sets a property in memory - use save() to write all properties back to the file.
 void setProperty(java.lang.String name, byte value)
          Sets a property in memory - use save() to write all properties back to the file.
 void setProperty(java.lang.String name, char value)
          Sets a property in memory - use save() to write all properties back to the file.
 void setProperty(java.lang.String name, double value)
          Sets a property in memory - use save() to write all properties back to the file.
 void setProperty(java.lang.String name, float value)
          Sets a property in memory - use save() to write all properties back to the file.
 void setProperty(java.lang.String name, int value)
          Sets a property in memory - use save() to write all properties back to the file.
 void setProperty(java.lang.String name, long value)
          Sets a property in memory - use save() to write all properties back to the file.
 void setProperty(java.lang.String name, java.lang.String value)
          Sets a property in memory - use save() to write all properties back to the file.
 java.lang.String toString()
          Returns the path to the file specified in the constructor and all properties currently in memory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IniFile

public IniFile(java.lang.String fileName)
Create a new instance of IniFile. For existing files, need to call read() method to load the contents from the file.

Parameters:
fileName - Full path of ini file to read/write.
Method Detail

read

public void read()
          throws java.io.IOException
Reads the file specified in the constuctor.

Throws:
java.io.IOException - Exception thrown if the file does not exist or another I/O error occurs.

save

public void save(java.lang.String header)
          throws java.io.IOException
Saves settings to file specified in the constuctor. Overwrites old file if it exists. Creates the directory if the specified directory does not exist.

Parameters:
header - New header for ini file.
Throws:
java.io.IOException - Exception thrown if an I/O error occurs.

clear

public void clear()
Clears out all loaded properties, does not modify the underlying file.


setProperty

public void setProperty(java.lang.String name,
                        java.lang.String value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

setProperty

public void setProperty(java.lang.String name,
                        int value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

setProperty

public void setProperty(java.lang.String name,
                        boolean value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

setProperty

public void setProperty(java.lang.String name,
                        long value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

setProperty

public void setProperty(java.lang.String name,
                        float value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

setProperty

public void setProperty(java.lang.String name,
                        double value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

setProperty

public void setProperty(java.lang.String name,
                        char value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

setProperty

public void setProperty(java.lang.String name,
                        byte value)
Sets a property in memory - use save() to write all properties back to the file.

Parameters:
name - The name (key) of the setting.
value - The value of the setting.

getProperty

public java.lang.String getProperty(java.lang.String name,
                                    java.lang.String defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

getProperty

public int getProperty(java.lang.String name,
                       int defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

getProperty

public boolean getProperty(java.lang.String name,
                           boolean defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

getProperty

public double getProperty(java.lang.String name,
                          double defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

getProperty

public byte getProperty(java.lang.String name,
                        byte defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

getProperty

public long getProperty(java.lang.String name,
                        long defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

getProperty

public float getProperty(java.lang.String name,
                         float defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

getProperty

public char getProperty(java.lang.String name,
                        char defaultValue)
Returns a property from memory.

Parameters:
name - The name (key) of the property to retrieve.
defaultValue - The value to return if property is not found.
Returns:
The value of the property if found, else the default value.

toString

public java.lang.String toString()
Returns the path to the file specified in the constructor and all properties currently in memory.

Overrides:
toString in class java.lang.Object
Returns:
The path to the file specified in the constructor and all properties currently in memory.


Copyright © 2000-2009 Hugues Johnson