Class IniFile
Class IniFile
java.lang.Object
|
+----IniFile
- public class IniFile
- extends Object
A class for handling Windows-style INI files. The file format is as
follows:
- [subject] - anything beginning with [ and ending with ] is a subject
- ;comment - anything beginning with a ; is a comment
- variable=value - anything of the format string=string is an assignment
- comment - anything that doesn't match any of the above is a comment
- Author:
- Steve DeGroof, degroof@mindspring.com, http://www.mindspring.com/~degroof
-
fileName
- Name of the file
-
lines
- Actual text lines of the file stored in a vector.
-
saveOnChange
- If true, INI file will be saved every time a value is changed.
-
subjects
- A vector of all subjects
-
values
- A vector of variable value vectors grouped by subject
-
variables
- A vector of variable name vectors grouped by subject
-
IniFile(String)
- Creates an INI file object using the specified name
If the named file doesn't exist, create one
-
IniFile(String, boolean)
- Creates an INI file object using the specified name
If the named file doesn't exist, create one
-
addAssignment(String, String)
- Adds and assignment (i.e.
-
addSubjectLine(String)
- add a subject line to the end of the lines vector
-
addValue(String, String, String, boolean)
- Sets a specific subject/variable combination the given value.
-
createFile()
- Create a new INI file.
-
deleteSubject(String)
- delete a subject and all its variables
-
deleteValue(String, String)
- delete variable within a subject
-
endOfSubject(int)
- find the line number which is 1 past the last assignment in a subject
starting at a given line
-
finalize()
- clean up
-
findAssignmentBetween(String, int, int)
- find the line containing a variable within a range of lines
-
findAssignmentLine(String, String)
- find the line containing a variable within a subject
-
findSubjectLine(String)
- find a subject line within the lines vector
-
getLines()
- get a copy of the lines vector
-
getSubjects()
- get an array containing all subjects
-
getValue(String, String)
- get the value of a variable within a subject
-
getVariables(String)
- get a vector containing all variables in a subject
-
isanAssignment(String)
- does the line represent an assignment?
-
isaSubject(String)
- does the line represent a subject?
-
loadFile()
- Loads and parses the INI file.
-
main(String[])
-
-
parseLines()
- Reads lines, filling in subjects, variables and values.
-
saveFile()
- save the lines vector back to the INI file
-
setLine(String, String, String)
- set a line in the lines vector
-
setValue(String, String, String)
- Sets a specific subject/variable combination the given value.
lines
protected Vector lines
- Actual text lines of the file stored in a vector.
subjects
protected Vector subjects
- A vector of all subjects
variables
protected Vector variables
- A vector of variable name vectors grouped by subject
values
protected Vector values
- A vector of variable value vectors grouped by subject
fileName
protected String fileName
- Name of the file
saveOnChange
protected boolean saveOnChange
- If true, INI file will be saved every time a value is changed. Defaults to false
IniFile
public IniFile(String name)
- Creates an INI file object using the specified name
If the named file doesn't exist, create one
- Parameters:
- name - the name of the file
IniFile
public IniFile(String name,
boolean save)
- Creates an INI file object using the specified name
If the named file doesn't exist, create one
- Parameters:
- name - the name of the file
- saveOnSet - save file whenever a value is set
loadFile
public void loadFile()
- Loads and parses the INI file. Can be used to reload from file.
createFile
protected boolean createFile()
- Create a new INI file.
parseLines
protected void parseLines()
- Reads lines, filling in subjects, variables and values.
addAssignment
protected boolean addAssignment(String subject,
String assignment)
- Adds and assignment (i.e. "variable=value") to a subject.
setValue
public boolean setValue(String subject,
String variable,
String value)
- Sets a specific subject/variable combination the given value. If the subject
doesn't exist, create it. If the variable doesn't exist, create it. If
saveOnChange is true, save the file;
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- variable - the variable name (e.g. "Color")
- value - the value of the variable (e.g. "green")
- Returns:
- true if successful
addValue
protected boolean addValue(String subject,
String variable,
String value,
boolean addToLines)
- Sets a specific subject/variable combination the given value. If the subject
doesn't exist, create it. If the variable doesn't exist, create it.
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- variable - the variable name (e.g. "Color")
- value - the value of the variable (e.g. "green")
- addToLines - add the information to the lines vector
- Returns:
- true if successful
isaSubject
protected boolean isaSubject(String line)
- does the line represent a subject?
- Parameters:
- line - a string representing a line from an INI file
- Returns:
- true if line is a subject
setLine
protected void setLine(String subject,
String variable,
String value)
- set a line in the lines vector
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- variable - the variable name (e.g. "Color")
- value - the value of the variable (e.g. "green")
findAssignmentLine
protected int findAssignmentLine(String subject,
String variable)
- find the line containing a variable within a subject
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- variable - the variable name (e.g. "Color")
- Returns:
- the line number of the assignment, -1 if not found
findAssignmentBetween
protected int findAssignmentBetween(String variable,
int start,
int end)
- find the line containing a variable within a range of lines
- Parameters:
- variable - the variable name (e.g. "Color")
- start - the start of the range (inclusive)
- end - the end of the range (exclusive)
- Returns:
- the line number of the assignment, -1 if not found
addSubjectLine
protected void addSubjectLine(String subject)
- add a subject line to the end of the lines vector
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
findSubjectLine
protected int findSubjectLine(String subject)
- find a subject line within the lines vector
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- Returns:
- the line number of the subject, -1 if not found
endOfSubject
protected int endOfSubject(int start)
- find the line number which is 1 past the last assignment in a subject
starting at a given line
- Parameters:
- start - the line number at which to start looking
- Returns:
- the line number of the last assignment + 1
isanAssignment
protected boolean isanAssignment(String line)
- does the line represent an assignment?
- Parameters:
- line - a string representing a line from an INI file
- Returns:
- true if line is an assignment
getLines
public Vector getLines()
- get a copy of the lines vector
getVariables
public String[] getVariables(String subject)
- get a vector containing all variables in a subject
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- Returns:
- a list of variables, empty vector if subject not found
getSubjects
public String[] getSubjects()
- get an array containing all subjects
- Returns:
- a list of subjects
getValue
public String getValue(String subject,
String variable)
- get the value of a variable within a subject
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- variable - the variable name (e.g. "Color")
- Returns:
- the value of the variable (e.g. "green"), empty string if not found
deleteValue
public void deleteValue(String subject,
String variable)
- delete variable within a subject
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
- variable - the variable name (e.g. "Color")
deleteSubject
public void deleteSubject(String subject)
- delete a subject and all its variables
- Parameters:
- subject - the subject heading (e.g. "Widget Settings")
saveFile
public void saveFile()
- save the lines vector back to the INI file
finalize
protected void finalize()
- clean up
- Overrides:
- finalize in class Object
main
public static void main(String args[])