kirk.util
Class FileLineList

java.lang.Object
  |
  +--kirk.util.FileLineList

public class FileLineList
extends java.lang.Object

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.

Version:
1
Author:
Berend "Kirk" Wouda

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

linelist

protected java.util.ArrayList linelist
The List of lines that were in the file when it was read.


writesupported

protected boolean writesupported
Indicates whether this FileLineList supports writing.

Constructor Detail

FileLineList

public FileLineList(java.lang.String filename,
                    boolean writable)
             throws java.io.IOException
Constructs a new FileLineList that reads from filename. Whether or not FileLineList can write is indicated by writable.

Parameters:
filename - The file to be read from.
writable - true if this FileLineList must allow editing methods, false otherwise.

FileLineList

public FileLineList(java.lang.String filename)
             throws java.io.IOException
Constructs a new FileLineList that reads from filename. This FileLineList can only be used to read lines from the file.

Parameters:
filename - The file to be read from.
Method Detail

getNumberOfLines

public int getNumberOfLines()
Retrieves the number of lines that were in the file.

Returns:
The number of lines that were in the file.

contains

public boolean contains(java.lang.String line)
Indicates whether the list contains the passed line.

Parameters:
line - The line to be searched for.
Returns:
true if the line was found, false otherwise.

firstIndexOf

public 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.

Parameters:
line - The line to be searched for.
Returns:
The index of the line, or -1 if it was not found.

lastIndexOf

public 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.

Parameters:
line - The line to be searched for.
Returns:
The index of the line, or -1 if it was not found.

getList

public 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. 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.

Parameters:
wildcard - The String containing wildcards that make out the selection pattern.
Returns:
An ArrayList with the results. All the results are Strings.

toArray

public java.lang.String[] toArray()
Converts this FileLineList into an array and returns that.

Returns:
The array of Strings that consists of all the lines that were in the file.

get

public java.lang.String get(int index)
Retrieves the line at the specified index.

Parameters:
index - The index of the line to be returned.
Returns:
The String at index.

add

public void add(java.lang.String line)
         throws WriteNotAllowedException
Add the passed line to the list.

Parameters:
line - The line to be added.
Throws:
WriteNotAllowedException - If writing is not allowed by this instance.

insert

public void insert(java.lang.String line,
                   int index)
            throws WriteNotAllowedException
Insert the passed line into the list at the passed index. All lines after and including index move down one. The new line will be at index.

Parameters:
line - The line to be inserted.
index - The index of the line that is after the the inserted line after insertion.
Throws:
WriteNotAllowedException - If writing is not allowed by this instance.

replace

public void replace(int index,
                    java.lang.String line)
             throws WriteNotAllowedException
Replaces the line at index with line. Note that these lines shouldn't end with newline characters or anything. This class takes care of that.

Parameters:
index - The index of the line to be replaced.
line - The line that will replace the current line.
Throws:
WriteNotAllowedException - If writing is not allowed by this instance.

append

public void append(int index,
                   java.lang.String line)
            throws WriteNotAllowedException
Appends the line at index with line. Note that these lines shouldn't end with newline characters or anything. This class takes care of that.

Parameters:
index - The index of the line to be appended.
line - The line that will append the current line.
Throws:
WriteNotAllowedException - If writing is not allowed by this instance.

remove

public void remove(int index)
            throws WriteNotAllowedException
Removes the line at index.

Parameters:
index - The index of the line to be removed.
Throws:
WriteNotAllowedException - If writing is not allowed by this instance.

flush

public void flush(java.lang.String filename)
           throws java.io.IOException
Writes the lines in this list to the passed file, if allowed.

Parameters:
filename - The file to be written to.
java.io.IOException

isWritable

public boolean isWritable()
Retrieves whether this FileLineList supports writing or not.

Returns:
boolean Whether this FileLineList supports writing or not.

setWritable

protected void setWritable(boolean writable)
Sets whether this FileLineList supports writing or not.

Parameters:
writable - Whether this FileLineList supports writing or not.