common
Class TabularReader

java.lang.Object
  extended by common.TabularReader

public class TabularReader
extends java.lang.Object

Reads tabular files, that is, files where each line represents a row, and each row is broken up into fields (columns) by separator characters. For example, a comma-separated values (CSV) file is a tabular file where the separator character is a comma. Other common separator characters are tabs and colons. A TabularReader supports reading one line at a time. As with BufferedReader.readLine, a line is terminated by "\r", "\n", or "\r\n" (or the end of the file). Each line is returned in the form of an array of Strings, where each String is a field. Fields may be empty (if a line contains two consecutive separator characters) and the number of fields may vary from line to line. By default, a TabularReader handles empty lines just like any other line (a blank line yields a zero-length array of fields). But if you use the three-argument constructor with skipEmptyLines set to true, it behaves as if empty lines were not in the file.


Constructor Summary
TabularReader(java.io.InputStream stream, char sep)
          Create a new TabularReader that reads from the given input stream and uses the given separator character.
TabularReader(java.io.InputStream stream, char sep, boolean skipEmptyLines)
          Create a new TabularReader that reads from the given input stream and uses the given separator character.
 
Method Summary
 void close()
          Close the underlying stream.
static void main(java.lang.String[] args)
          Test program: given a filename, read it as a tabular file and print it in a comma-separated format.
 java.lang.String[] peek()
          Returns an array of strings representing the fields of the next line in the input stream, but does not move on to the following line.
 java.lang.String[] readLine()
          Returns an array of strings representing the fields of the next line in the input stream, and moves on to the line after that.
 boolean ready()
          Returns true if there is another line to read from the input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TabularReader

public TabularReader(java.io.InputStream stream,
                     char sep)
              throws java.io.IOException
Create a new TabularReader that reads from the given input stream and uses the given separator character.

Throws:
java.io.IOException

TabularReader

public TabularReader(java.io.InputStream stream,
                     char sep,
                     boolean skipEmptyLines)
              throws java.io.IOException
Create a new TabularReader that reads from the given input stream and uses the given separator character. If skipEmptyLines is true, this reader will act as if empty lines were not in the file.

Throws:
java.io.IOException
Method Detail

close

public void close()
           throws java.io.IOException
Close the underlying stream.

Throws:
java.io.IOException

ready

public boolean ready()
Returns true if there is another line to read from the input stream.


readLine

public java.lang.String[] readLine()
                            throws java.io.IOException
Returns an array of strings representing the fields of the next line in the input stream, and moves on to the line after that.

Throws:
java.io.IOException

peek

public java.lang.String[] peek()
                        throws java.io.IOException
Returns an array of strings representing the fields of the next line in the input stream, but does not move on to the following line.

Throws:
java.io.IOException

main

public static void main(java.lang.String[] args)
Test program: given a filename, read it as a tabular file and print it in a comma-separated format.