textfile
Class DelimitedTextFile

java.lang.Object
  extended bytextfile.DelimitedTextFile

public class DelimitedTextFile
extends java.lang.Object

A class which can locate and read a delimited text file and make the fields available line by line. By default, the file is assumed to be tab-delimited (has tabs separating the string fields on each line). Alternatively, another delimiter can be designated. There can be any number of fields on each line. Each field is taken to be a String. Within a field, no trimming or parsing is done. This class is just a wrapper for a DelimitedTextReader.


Field Summary
static java.lang.String DEFAULT_DELIMITER
          The default delimiter used for splitting lines of text.
 
Constructor Summary
DelimitedTextFile(java.lang.String filename)
          Given a file name, find the file it refers to it and make it ready for read-access; the field delimiter is set to be the TAB character.
DelimitedTextFile(java.lang.String filename, java.lang.String delimiter)
          Given a file name, find the file it refers to it and make it ready for read-access; the field delimiter is set to be the parameter value.
 
Method Summary
 java.lang.String getFileName()
          Tells the name of the underlying file; may be a path or URL.
 int getLineNum()
          Tells the number of the most recently read line.
 java.lang.String[] readLineAsFieldArray()
          Read the next line of the file and parse it into fields.
 java.util.List readLineAsFieldList()
          Return the data of the current line as a list of Strings; see readLineAsFieldArray().
 java.lang.String toString()
          Produce a summary of this object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_DELIMITER

public static final java.lang.String DEFAULT_DELIMITER
The default delimiter used for splitting lines of text.

See Also:
Constant Field Values
Constructor Detail

DelimitedTextFile

public DelimitedTextFile(java.lang.String filename)
Given a file name, find the file it refers to it and make it ready for read-access; the field delimiter is set to be the TAB character. For more information, see the 2-argument constructor.

Parameters:
filename - a full or partial file name or URL.

DelimitedTextFile

public DelimitedTextFile(java.lang.String filename,
                         java.lang.String delimiter)
Given a file name, find the file it refers to it and make it ready for read-access; the field delimiter is set to be the parameter value. Look first on the class path; after that, bring up a file chooser as a last resort. If no file can be opened, or there is an error on the file, then a console message is printed; in this case the DelimitedTextFile object is effectively an empty file -- any calls to retrieve data will return null.

Parameters:
filename - a full or partial filename or URL; may not be empty or null.
delimiter - the string which delimits fields on each line of the file. This is typically a single character, such as TAB, but it can be any regular expression; may not be empty or null.
Throws:
java.lang.IllegalArgumentException - if a parameter value is bad.
Method Detail

readLineAsFieldArray

public java.lang.String[] readLineAsFieldArray()
Read the next line of the file and parse it into fields. Each field is terminated with the delimiter string for this file, or with end-of-file. The strings in the array may be empty but are never null. If the last character(s) on the line exactly match the delimiter string, then the last string of the array is the empty string.

Returns:
the fields of the file, as strings, or null if there is no more data, or if any error is encountered.

readLineAsFieldList

public java.util.List readLineAsFieldList()
Return the data of the current line as a list of Strings; see readLineAsFieldArray().

Returns:
a list with data equivalent to readLineAsFieldArray()

getLineNum

public int getLineNum()
Tells the number of the most recently read line.

Returns:
the line number of the most recently read line; the first line of the file is numbered 1; a return value of 0 means no lines have been read yet.

getFileName

public java.lang.String getFileName()
Tells the name of the underlying file; may be a path or URL.

Returns:
the identifier (full path or URL) of the underlying file, which might be different from the path or id as requested; return null if the file has not actually be located.

toString

public java.lang.String toString()
Produce a summary of this object.

Returns:
a string summarizing the status of this object.