public class IniFileManager
extends java.lang.Object
IniFileManager preserves comments
and blank lines as well as the order of sections and lines in general.
IniFileManager assumes configuration files to be split in
sections. A section starts out with a header, which consists of the section
name enclosed in brackets ('[' and ']'). Everything
before the first section header is ignored when loading from a stream or
file. The class can be used to
load configuration files without sections (ie Java-style properties).
IniFileManager.Section
A "common section" may be named. All sections inherit the options of this section but can overwrite them.
IniFileManager represents an INI file (or rather, its sections)
line by line, as comment, blank and option lines. A comment is a line which
has a comment delimiter as its first non-white space character. The default
comment delimiters, which may be overwritten, are '#' and
';'.
A blank line is any line that consists only of white space.
Everything else is an option line. Option names and values are separated by
option delimiters '=', ':' or white space (spaces
and tabs).
Here's a minimal example. Suppose, we have this in a file called
users.ini:
[root] role = administrator last_login = 2003-05-04 [joe] role = author last_login = 2003-05-13Let's load that file, add something to it and save the changes:
IniFileManager users = new IniFileManager();
users.load("users.ini");
users.set("root", "last_login", "2003-05-16");
users.addComment("root", "Must change password often");
users.set("root", "change_pwd", "10 days");
users.addBlankLine("root");
users.save("users.ini");
Now, the file looks like this:
[root] role = administrator last_login = 2003-05-16 # Must change password often change_pwd = 10 days [joe] role = author last_login = 2003-05-13
IniFileManager provides services simliar to the standard Java API class
java.util.Properties. It uses its own parser, though, which
differs in these respects from that of Properties:
#load(InputStreamReader) and
#save(OutputStreamWriter) methods with a reader and writer tuned to
the desired character encoding.'=', ':' and white space).| Modifier and Type | Class and Description |
|---|---|
static class |
IniFileManager.NoSuchSectionException
Thrown when an inexistent section is addressed.
|
static class |
IniFileManager.Section
Loads, edits and saves a section of an INI-style configuration file.
|
| Constructor and Description |
|---|
IniFileManager(java.io.File file)
Constructs new IniFileManager instance with a common section, defining
comment delimiters.
|
IniFileManager(java.lang.String filename) |
| Modifier and Type | Method and Description |
|---|---|
void |
addComment(java.lang.String section,
java.lang.String comment)
Adds a comment line to the end of a section.
|
boolean |
addSection(java.lang.String name)
Adds a section if it doesn't exist yet.
|
boolean |
clearSection(java.lang.String section)
Clears all options of a section.
|
java.lang.String |
get(java.lang.String section,
java.lang.String option)
Returns the value of a given option in a given section or null if either
the section or the option don't exist.
|
java.util.List<java.lang.String> |
getList(java.lang.String section)
Returns a list containing the values of all numbered options of this
section.
|
java.util.List<java.lang.String> |
getSectionNames()
Returns all section names in this instance .
|
boolean |
hasOption(java.lang.String section,
java.lang.String option)
Checks whether an option exists in a given section.
|
boolean |
hasSection(java.lang.String name)
Checks whether a section with a particular name exists in this instance.
|
java.util.List<java.lang.String> |
optionNames(java.lang.String section)
Returns all option names of a section.
|
boolean |
remove(java.lang.String section,
java.lang.String option)
Removes an option from a section if it exists.
|
void |
removeAllSections() |
boolean |
removeSection(java.lang.String name)
Removes a section if it exists.
|
void |
save()
Writes this instance in INI format to a file.
|
void |
set(java.lang.String section,
java.lang.String option,
java.lang.String value)
Sets the value of an option in a section, if the option exist, otherwise
adds the option to the section.
|
void |
setList(java.lang.String section,
java.util.List<java.lang.String> values)
Sets the values in a section.
|
void |
setOptionFormatString(java.lang.String formatString)
Sets the option format for this instance to the given string.
|
public IniFileManager(java.io.File file)
throws java.io.IOException
file - The ini File.java.io.IOExceptionpublic IniFileManager(java.lang.String filename)
throws java.io.IOException
java.io.IOExceptionpublic void setOptionFormatString(java.lang.String formatString)
%s three times, these will be replaced with the
option name, the option separator and the option value in this order.
Literal percentage signs must be escaped by preceding them with another
percentage sign (i.e., %% corresponds to one percentage sign).
The default format string is "%s %s %s".
Option formats may look like format strings as supported by Java 1.5, but
the string is in fact parsed in a custom fashion to guarantee backwards
compatibility. So don't try clever stuff like using format conversion types
other than %s.formatString - a format string, containing %s exactly three timesjava.lang.IllegalArgumentException - if the format string is illegalpublic java.lang.String get(java.lang.String section,
java.lang.String option)
section - the section's nameoption - the option's namejava.lang.NullPointerException - any of the arguments is nullpublic void set(java.lang.String section,
java.lang.String option,
java.lang.String value)
section - the section's nameoption - the option's namevalue - the option's valuejava.lang.IllegalArgumentException - the option name is illegal, ie contains a '=' character or
consists only of white spacejava.lang.NullPointerException - section or option are nullpublic boolean remove(java.lang.String section,
java.lang.String option)
section - the section's nameoption - the option's nametrue if the option was actually removedIniFileManager.NoSuchSectionException - no section with the given name existspublic boolean hasOption(java.lang.String section,
java.lang.String option)
section - the section's nameoption - the option's namepublic boolean hasSection(java.lang.String name)
name - the name of the sectionpublic boolean addSection(java.lang.String name)
name - the name of the sectiontrue if the section didn't already existjava.lang.IllegalArgumentException - the name is illegal, ie contains one of the characters '[' and
']' or consists only of white spacepublic boolean removeSection(java.lang.String name)
name - the section's nametrue if the section actually existedjava.lang.IllegalArgumentException - when trying to remove the common sectionpublic void removeAllSections()
public boolean clearSection(java.lang.String section)
section - the section's nametrue if the section actually existedpublic void setList(java.lang.String section,
java.util.List<java.lang.String> values)
section - the section's namevalues - the valuesjava.lang.NullPointerException - section is nullpublic java.util.List<java.lang.String> getList(java.lang.String section)
section - the section's namepublic java.util.List<java.lang.String> getSectionNames()
public java.util.List<java.lang.String> optionNames(java.lang.String section)
section - the section's nameIniFileManager.NoSuchSectionException - no section with the given name existspublic void addComment(java.lang.String section,
java.lang.String comment)
section - the section's namecomment - the commentIniFileManager.NoSuchSectionException - no section with the given name existspublic void save()
throws java.io.IOException
java.io.IOException - at an I/O problem