|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--kirk.util.FileLineList
This class reads a file line by line into an Arraylist
.
Various methods are supplied to retrieve the lines.
The power of this class lies in the fact that text files can be randomly accessed,
in a line by line fashion.
Take note that the entire file is read upon construction. This also means no files
will be held while this class is active, and no closing is required. This has as
side-effect that the file can be changed by external means while this class is
active.
Lines can also be changed, added, or removed, after which the
flush(String filename)
method will write all changes to a file. In
order to be able to do this, the
FileLineList(String filename, boolean writable)
constructor must be
called with writable == true
upon object construction.
Seeing that this class allows the file it originally read from to be changed while
this class is active, any changes made to that file during that time will be lost
when the list of lines is flushed to that file. However, you can, if you wish,
flush the list to another file instead.
You always need to supply a filename when calling the flush method, though.
Field Summary | |
protected ArrayList |
linelist
The List of lines that were in the file when it was read. |
protected boolean |
writesupported
Indicates whether this FileLineList supports writing. |
Constructor Summary | |
FileLineList(String filename)
Constructs a new FileLineList that reads from
filename . |
|
FileLineList(String filename,
boolean writable)
Constructs a new FileLineList that reads from
filename . |
Method Summary | |
void |
add(String line)
Add the passed line to the list. |
void |
append(int index,
String line)
Appends the line at index with line .
|
boolean |
contains(String line)
Indicates whether the list contains the passed line. |
int |
firstIndexOf(String line)
Checks whether the list contains the passed line, and returns the index of the first occurence of that line. |
void |
flush(String filename)
Writes the lines in this list to the passed file, if allowed. |
String |
get(int index)
Retrieves the line at the specified index. |
ArrayList |
getList(String wildcard)
Returns a list (in the form of an ArrayList ) with all the lines
that comply with the passed pattern.
|
int |
getNumberOfLines()
Retrieves the number of lines that were in the file. |
void |
insert(String line,
int index)
Insert the passed line into the list at the passed index. |
boolean |
isWritable()
Retrieves whether this FileLineList supports writing or not. |
int |
lastIndexOf(String line)
Checks whether the list contains the passed line, and returns the index of the last occurence of that line. |
void |
remove(int index)
Removes the line at index . |
void |
replace(int index,
String line)
Replaces the line at index with line .
|
protected void |
setWritable(boolean writable)
Sets whether this FileLineList supports writing or not. |
String[] |
toArray()
Converts this FileLineList into an Array and returns
that. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected ArrayList linelist
protected boolean writesupported
FileLineList
supports writing.
Constructor Detail |
public FileLineList(String filename, boolean writable) throws IOException
FileLineList
that reads from
filename
. Whether or not FileLineList
can write is
indicated by writable
.
filename
- The file to be read from.writable
- true if this FileLineList
must allow editing
methods, false otherwise.public FileLineList(String filename) throws IOException
FileLineList
that reads from
filename
. This FileLineList
can only be used to read
lines from the file.
filename
- The file to be read from.Method Detail |
public int getNumberOfLines()
public boolean contains(String line)
line
- The line to be searched for.
true
if the line was found, false
otherwise.public int firstIndexOf(String line)
line
- The line to be searched for.
public int lastIndexOf(String line)
line
- The line to be searched for.
public ArrayList getList(String wildcard)
ArrayList
) with all the lines
that comply with the passed pattern.
Note: Any changes made to the returned list will not be reflected in
this object.
* can be used to indicate any amount of characters, and
? can be used to indicate a single character.
wildcard
- The String
containing wildcards that make out the
selection pattern.
Strings
.public String[] toArray()
FileLineList
into an Array
and returns
that.
Array
of String
s that consists of all
the lines that were in the file.public String get(int index)
index
- The index of the line to be returned.
index
.public void add(String line) throws WriteNotAllowedException
line
- The line to be added.
WriteNotAllowedException
- If writing is not allowed by this instance.public void insert(String line, int index) throws WriteNotAllowedException
index
move down one.
The new line will be at index
.
line
- The line to be inserted.index
- The index of the line that is after the the inserted line after
insertion.
WriteNotAllowedException
- If writing is not allowed by this instance.public void replace(int index, String line) throws WriteNotAllowedException
index
with line
.
Note that these lines shouldn't end with newline characters or anything.
This class takes care of that.
index
- The index of the line to be replaced.line
- The line that will replace the current line.
WriteNotAllowedException
- If writing is not allowed by this instance.public void append(int index, String line) throws WriteNotAllowedException
index
with line
.
Note that these lines shouldn't end with newline characters or anything.
This class takes care of that.
index
- The index of the line to be appended.line
- The line that will append the current line.
WriteNotAllowedException
- If writing is not allowed by this instance.public void remove(int index) throws WriteNotAllowedException
index
.
index
- The index of the line to be removed.
WriteNotAllowedException
- If writing is not allowed by this instance.public void flush(String filename) throws IOException, WriteNotAllowedException
filename
- The file to be written to.
IOException
WriteNotAllowedException
public boolean isWritable()
FileLineList
supports writing or not.
FileLineList
supports writing or
not.protected void setWritable(boolean writable)
FileLineList
supports writing or not.
writable
- Whether this FileLineList
supports writing or
not.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |