|
|||||||||||
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(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 java.util.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(java.lang.String filename)
Constructs a new FileLineList that reads from filename .
|
|
FileLineList(java.lang.String filename,
boolean writable)
Constructs a new FileLineList that reads from filename .
|
Method Summary | |
void |
add(java.lang.String line)
Add the passed line to the list. |
void |
append(int index,
java.lang.String line)
Appends the line at index with line .
|
boolean |
contains(java.lang.String line)
Indicates whether the list contains the passed line. |
int |
firstIndexOf(java.lang.String line)
Checks whether the list contains the passed line, and returns the index of the first occurence of that line. |
void |
flush(java.lang.String filename)
Writes the lines in this list to the passed file, if allowed. |
java.lang.String |
get(int index)
Retrieves the line at the specified index. |
java.util.ArrayList |
getList(java.lang.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(java.lang.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(java.lang.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,
java.lang.String line)
Replaces the line at index with line .
|
protected void |
setWritable(boolean writable)
Sets whether this FileLineList supports writing or not. |
java.lang.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 java.util.ArrayList linelist
protected boolean writesupported
Constructor Detail |
public FileLineList(java.lang.String filename, boolean writable) throws java.io.IOException
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(java.lang.String filename) throws java.io.IOException
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(java.lang.String line)
line
- The line to be searched for.
public int firstIndexOf(java.lang.String line)
line
- The line to be searched for.
public int lastIndexOf(java.lang.String line)
line
- The line to be searched for.
public java.util.ArrayList getList(java.lang.String wildcard)
wildcard
- The String containing wildcards that make out the selection
pattern.
public java.lang.String[] toArray()
public java.lang.String get(int index)
index
- The index of the line to be returned.
index
.public void add(java.lang.String line) throws WriteNotAllowedException
line
- The line to be added.
WriteNotAllowedException
- If writing is not allowed by this instance.public void insert(java.lang.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, java.lang.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, java.lang.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(java.lang.String filename) throws java.io.IOException
filename
- The file to be written to.
java.io.IOException
public boolean isWritable()
protected void setWritable(boolean writable)
writable
- Whether this FileLineList supports writing or not.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |