Holger's
Java API

com.antelmann.io
Class IniFile

java.lang.Object
  extended by java.io.File
      extended by com.antelmann.io.IniFile
All Implemented Interfaces:
Serializable, Comparable<File>

public class IniFile
extends File

a file that loads properties to standard *.ini conventions (unlike the class java.util.Properties)

Author:
Holger Antelmann
See Also:
Serialized Form

Field Summary
protected  String charset
          uses the default charset of this Java virtual machine by default
protected  char[] commentIndicator
          uses '#', ';' and '[' by default, the array must never be empty
protected  String fieldSeparator
          uses the equal sign '=' by default
 
Fields inherited from class java.io.File
pathSeparator, pathSeparatorChar, separator, separatorChar
 
Constructor Summary
IniFile(File file)
           
IniFile(File file, String charset)
           
 
Method Summary
 void addProperty(String key, String value)
           
protected  boolean isCommentLine(String line)
          checks whether the line is considered a comment.
protected  String[] parseLine(String line)
          returns either null or an array of size 2, containing key/value pair.
 Properties readCascadingBasedOnFileNameProperty(String fileProperty)
          the properties are read recursively where the given fileProperty is treated as the file name for a parent IniFile.
 Properties readProperties()
           
static Properties readProperties(InputStream in)
          uses default values for comment indicators and field separators
static Properties readProperties(InputStream in, String charset)
          uses default values for comment indicators and field separators
 boolean removeProperty(String property)
          reads the complete file into memory, removes only the given property line (if it existed) and writes the content back to the file.
 boolean replaceProperty(String property, String newValue)
           
 boolean replaceProperty(String property, String newProperty, String newValue)
          reads the complete file into memory, replaces only the given property line (if it existed) and writes the content back to the file.
 IniFile setCommentIndicator(char... commentIndicator)
          returns this
 IniFile setFieldSeparator(String fieldSeparator)
          returns this
 void store(Properties props, String comment)
           
 void writeProperties(Properties props)
           
 
Methods inherited from class java.io.File
canExecute, canRead, canWrite, compareTo, createNewFile, createTempFile, createTempFile, delete, deleteOnExit, equals, exists, getAbsoluteFile, getAbsolutePath, getCanonicalFile, getCanonicalPath, getFreeSpace, getName, getParent, getParentFile, getPath, getTotalSpace, getUsableSpace, hashCode, isAbsolute, isDirectory, isFile, isHidden, lastModified, length, list, list, listFiles, listFiles, listFiles, listRoots, mkdir, mkdirs, renameTo, setExecutable, setExecutable, setLastModified, setReadable, setReadable, setReadOnly, setWritable, setWritable, toString, toURI, toURL
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

fieldSeparator

protected String fieldSeparator
uses the equal sign '=' by default


commentIndicator

protected char[] commentIndicator
uses '#', ';' and '[' by default, the array must never be empty


charset

protected String charset
uses the default charset of this Java virtual machine by default

Constructor Detail

IniFile

public IniFile(File file)

IniFile

public IniFile(File file,
               String charset)
Method Detail

setFieldSeparator

public IniFile setFieldSeparator(String fieldSeparator)
returns this


setCommentIndicator

public IniFile setCommentIndicator(char... commentIndicator)
returns this


writeProperties

public void writeProperties(Properties props)
                     throws IOException
Throws:
IOException

readProperties

public Properties readProperties()
                          throws IOException
Throws:
IOException

readProperties

public static Properties readProperties(InputStream in)
                                 throws IOException
uses default values for comment indicators and field separators

Throws:
IOException

readProperties

public static Properties readProperties(InputStream in,
                                        String charset)
                                 throws IOException
uses default values for comment indicators and field separators

Throws:
IOException

readCascadingBasedOnFileNameProperty

public Properties readCascadingBasedOnFileNameProperty(String fileProperty)
                                                throws IOException
the properties are read recursively where the given fileProperty is treated as the file name for a parent IniFile. Properties in this IniFile would override properties of a parent file.

Throws:
IOException

parseLine

protected String[] parseLine(String line)
returns either null or an array of size 2, containing key/value pair. Note that if the key is zero-length, null is returned instead of the array. If the value is zero-length, the key is returned at index 0 of the array, whereas the value is then returned as an empty string at index 1. I.e. if a non-null value is returned, the elements of the array are also non-null. In case the line is empty or considered a comment, null is returned. If This method checks first whether the given line is a comment line; if so, null is returned.

See Also:
isCommentLine(String)

isCommentLine

protected boolean isCommentLine(String line)
checks whether the line is considered a comment. A line is considered a comment if it is either null, zero-length or it starts with a character in the commentIndicator.


store

public void store(Properties props,
                  String comment)
           throws IOException
Throws:
IOException

replaceProperty

public boolean replaceProperty(String property,
                               String newValue)
                        throws IOException
Throws:
IOException

replaceProperty

public boolean replaceProperty(String property,
                               String newProperty,
                               String newValue)
                        throws IOException
reads the complete file into memory, replaces only the given property line (if it existed) and writes the content back to the file.

Throws:
IOException

addProperty

public void addProperty(String key,
                        String value)
                 throws IOException
Throws:
IOException

removeProperty

public boolean removeProperty(String property)
                       throws IOException
reads the complete file into memory, removes only the given property line (if it existed) and writes the content back to the file.

Throws:
IOException


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads