Package util

Class TableReader

java.lang.Object
util.TableReader

public class TableReader extends Object
Class for readers for files with tabular data.

The input stream is split into fields (table cell) and records (table row). Three types/classes of characters are used for this: record separators, field separators, and blank characters. The latter are used to fill table fields to a given width (for example, in order to align the fields in a file) and are removed when a field is read.

In addition, comment characters can be registered. They are used to identify comment records by checking whether a record starts with such a comment character.

Finally, null value characters can be registered.

Since:
2006.10.05
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    class/type flag: blank character
    static final int[]
    the codes of the character classes
    static final int
    class/type flag: comment character
    int
    the last delimiter read: -1 if end of file/input, 0 if field separator, 1 if record separator
    the next table field
    static final int
    class/type flag: field separator
    static final int
    class/type flag: null value character
    static final int
    class/type flag: other character type
    int
    the current record number
    static final int
    class/type flag: record separator
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a table reader with default character flags.
    Create a table reader with default character flags.
    Create a table reader with default character flags.
    Create a table reader with default character flags.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    Close the underlying stream.
    final int
    getTypes(char c)
    Get the classes/types of a given character.
    final boolean
    isType(int type, char c)
    Check whether a given character is in a given class or of a given type.
    static void
    main(String[] args)
    Main function for testing basic functionality.
    final int
    Get the next field/cell of the table.
    rno()
    Get a string stating the current record number.
    final String
    rno(int offset)
    Get a string stating the current record number.
    final void
    setChars(int type, String chars)
    Set the characters for a specific type/class.
    final void
    setChars(String recseps, String fldseps, String blanks, String nullchs, String comment)
    Set the characters for all standard types.
    final void
    setCharsCoded(String recseps, String fldseps, String blanks, String nullchs, String comment)
    Set the characters for all standard types.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • RECSEP

      public static final int RECSEP
      class/type flag: record separator
      See Also:
    • FLDSEP

      public static final int FLDSEP
      class/type flag: field separator
      See Also:
    • BLANK

      public static final int BLANK
      class/type flag: blank character
      See Also:
    • NULL

      public static final int NULL
      class/type flag: null value character
      See Also:
    • COMMENT

      public static final int COMMENT
      class/type flag: comment character
      See Also:
    • OTHER

      public static final int OTHER
      class/type flag: other character type
      See Also:
    • codes

      public static final int[] codes
      the codes of the character classes
    • field

      public String field
      the next table field
    • delim

      public int delim
      the last delimiter read: -1 if end of file/input, 0 if field separator, 1 if record separator
    • record

      public int record
      the current record number
  • Constructor Details

    • TableReader

      public TableReader(Reader reader)
      Create a table reader with default character flags.

      By default the following character settings are used:
      record separators: "\n", field separators: " \t", blanks: " \r\t", null value characters: "?*".

      Parameters:
      reader - the reader to work on
      Since:
      2006.10.05 (Christian Borgelt)
    • TableReader

      public TableReader(String string)
      Create a table reader with default character flags.
      Parameters:
      string - the string to read
      Since:
      2006.10.05 (Christian Borgelt)
    • TableReader

      public TableReader(InputStream stream) throws IOException
      Create a table reader with default character flags.
      Parameters:
      stream - the input stream to read
      Throws:
      IOException - if a read error occurs
      Since:
      2006.10.05 (Christian Borgelt)
    • TableReader

      public TableReader(File file) throws IOException
      Create a table reader with default character flags.
      Parameters:
      file - the file to read
      Throws:
      IOException - if a read error occurs
      Since:
      2007.07.26 (Christian Borgelt)
  • Method Details

    • setChars

      public final void setChars(int type, String chars)
      Set the characters for a specific type/class.
      Parameters:
      type - the type/class of characters to set; must be one of the constants RECSEP, FLDSEP, BLANK, NULL, COMMENT, or OTHER (or a combination of these, by binary or)
      chars - the characters to set
      Since:
      2006.10.05 (Christian Borgelt)
    • setChars

      public final void setChars(String recseps, String fldseps, String blanks, String nullchs, String comment)
      Set the characters for all standard types.

      If a parameter is null, the corresponding character flags are maintained.

      Parameters:
      recseps - the record separators
      fldseps - the field separators
      blanks - the blank characters
      nullchs - the null value characters
      comment - the comment characters
      Since:
      2007.05.17 (Christian Borgelt)
    • setCharsCoded

      public final void setCharsCoded(String recseps, String fldseps, String blanks, String nullchs, String comment)
      Set the characters for all standard types.

      The strings are assumed to contain the characters in encoded form, so that they have to be decoded with an ASCIICoder. If a parameter is null, the corresponding character flags are maintained.

      Parameters:
      recseps - the record separators
      fldseps - the field separators
      blanks - the blank characters
      nullchs - the null value characters
      comment - the comment characters
      Since:
      2007.05.17 (Christian Borgelt)
    • isType

      public final boolean isType(int type, char c)
      Check whether a given character is in a given class or of a given type.
      Parameters:
      type - the type/class for which to query; must be one of the constants RECSEP, FLDSEP, BLANK, COMMENT, or OTHER.
      c - the character to query
      Returns:
      whether the character is in the given class
      Since:
      2006.10.06 (Christian Borgelt)
    • getTypes

      public final int getTypes(char c)
      Get the classes/types of a given character.
      Parameters:
      c - the character to query
      Returns:
      the classes character is in, as a combination of the flags RECSEP, FLDSEP, BLANK, COMMENT, and OTHER
      Since:
      2006.10.06 (Christian Borgelt)
    • rno

      public String rno()
      Get a string stating the current record number. Useful for error reporting.
      Returns:
      a string stating the current record number in the format "(record xxx)"
      Since:
      2007.01.31 (Christian Borgelt)
    • rno

      public final String rno(int offset)
      Get a string stating the current record number. Useful for error reporting.
      Parameters:
      offset - the offset to add to the record number
      Returns:
      a string stating the current record number in the format "(record xxx)"
      Since:
      2007.03.29 (Christian Borgelt)
    • nextField

      public final int nextField() throws IOException
      Get the next field/cell of the table.

      Note that a record separator is (virtually) inserted at the end of the file/input if the file/input does not end with a record separator.

      Returns:
      the type of the delimiter of the field read: -1, if end of file/input, 0, if field separator, +1, if record separator.
      Throws:
      IOException - if an I/O error occurs
      Since:
      2006.10.05 (Christian Borgelt)
    • close

      public final void close() throws IOException
      Close the underlying stream.
      Throws:
      IOException - if an I/O error occurs
      Since:
      2007.02.08 (Christian Borgelt)
    • main

      public static void main(String[] args)
      Main function for testing basic functionality.
      Parameters:
      args - the command line arguments
      Since:
      2004.10.06 (Christian Borgelt)