Class TableReader
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
FieldsModifier and TypeFieldDescriptionstatic final int
class/type flag: blank characterstatic final int[]
the codes of the character classesstatic final int
class/type flag: comment characterint
the last delimiter read: -1 if end of file/input, 0 if field separator, 1 if record separatorthe next table fieldstatic final int
class/type flag: field separatorstatic final int
class/type flag: null value characterstatic final int
class/type flag: other character typeint
the current record numberstatic final int
class/type flag: record separator -
Constructor Summary
ConstructorsConstructorDescriptionTableReader
(File file) Create a table reader with default character flags.TableReader
(InputStream stream) Create a table reader with default character flags.TableReader
(Reader reader) Create a table reader with default character flags.TableReader
(String string) Create a table reader with default character flags. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
close()
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 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
Set the characters for a specific type/class.final void
Set the characters for all standard types.final void
Set the characters for all standard types.
-
Field Details
-
RECSEP
public static final int RECSEPclass/type flag: record separator- See Also:
-
FLDSEP
public static final int FLDSEPclass/type flag: field separator- See Also:
-
BLANK
public static final int BLANKclass/type flag: blank character- See Also:
-
NULL
public static final int NULLclass/type flag: null value character- See Also:
-
COMMENT
public static final int COMMENTclass/type flag: comment character- See Also:
-
OTHER
public static final int OTHERclass/type flag: other character type- See Also:
-
codes
public static final int[] codesthe codes of the character classes -
field
the next table field -
delim
public int delimthe last delimiter read: -1 if end of file/input, 0 if field separator, 1 if record separator -
record
public int recordthe current record number
-
-
Constructor Details
-
TableReader
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
Create a table reader with default character flags.- Parameters:
string
- the string to read- Since:
- 2006.10.05 (Christian Borgelt)
-
TableReader
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
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
Set the characters for a specific type/class.- Parameters:
type
- the type/class of characters to set; must be one of the constantsRECSEP
,FLDSEP
,BLANK
,NULL
,COMMENT
, orOTHER
(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 separatorsfldseps
- the field separatorsblanks
- the blank charactersnullchs
- the null value characterscomment
- 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 isnull
, the corresponding character flags are maintained.- Parameters:
recseps
- the record separatorsfldseps
- the field separatorsblanks
- the blank charactersnullchs
- the null value characterscomment
- 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 constantsRECSEP
,FLDSEP
,BLANK
,COMMENT
, orOTHER
.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
, andOTHER
- Since:
- 2006.10.06 (Christian Borgelt)
-
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
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
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
Close the underlying stream.- Throws:
IOException
- if an I/O error occurs- Since:
- 2007.02.08 (Christian Borgelt)
-
main
Main function for testing basic functionality.- Parameters:
args
- the command line arguments- Since:
- 2004.10.06 (Christian Borgelt)
-