public class DelimitedTextParser
extends java.lang.Object
DelimitedTextParser
parses a line of delimited text input as a
String.
DelimitedTextParser is intended for use with ASCII-delimited text files, which typically contain one line per record and contain text fields delimited by commas (',') and enclosed by quotes ('"'). A string may contain both quoted and unquoted fields, assuming that none of the unquoted fields contain occurrences of the delimiter character. Input text may contain any Unicode characters. Quoting may be disabled if the input data does not contain fields enclosed by quotes. Note that the end of the string is treated as a delimiter (the last field should not be terminated by a delimiter!)
Returned output fields consist of the characters between delimiters and quotes (the delimiter and quote characters are not output). Empty fields (adjacent delimiters) are returned as zero-length Strings (example: "a,,b" will return "a", "", then "b").
Constructor and Description |
---|
DelimitedTextParser()
Default constructor.
|
DelimitedTextParser(char fieldDelimiter,
char fieldQuoter)
Constructs parser with specified quoter and delimiter parameters (note:
quoting is enabled by default).
|
DelimitedTextParser(java.lang.String lineIn)
Constructs parser for a String using default parse parameters.
|
DelimitedTextParser(java.lang.String lineIn,
char fieldDelimiter,
char fieldQuoter)
Constructs parser for a String using specified parse parameters (note:
quoting is enabled by default).
|
Modifier and Type | Method and Description |
---|---|
char |
getParseDelimiter()
Returns the parse delimiter character
|
char |
getParseQuoter()
Returns the parse quote character
|
java.lang.String |
getParseString()
Returns the original line being parsed.
|
static void |
main(java.lang.String[] args)
Test code -- just run, requires no command line params.
|
java.lang.String |
nextField()
Returns the next field from the input String.
|
java.util.List<java.lang.String> |
parseAll()
Returns a list of all fields, consuming the entire input String.
|
void |
resetParseDefaults()
Resets the parsing parameters to their default values: delimiter = comma,
quoter = quote, quoter is enabled.
|
void |
setParseDelimiter(char delimiter)
Sets the parse delimiter to a specified character.
|
void |
setParseQuoter(char quoter)
Sets the parse quoter to a specified character.
|
void |
setParseString(java.lang.String lineIn)
Loads a new String into an existing parser and prepares the parse object
for parsing the first field of the string.
|
void |
setQuoterEnabled(boolean quoterEnabled)
Enables or disables (turns on/off) quote parsing.
|
public DelimitedTextParser()
public DelimitedTextParser(java.lang.String lineIn) throws java.lang.IllegalArgumentException
lineIn
- String to be parsed.java.lang.IllegalArgumentException
- if an error occurredpublic DelimitedTextParser(char fieldDelimiter, char fieldQuoter)
fieldDelimiter
- Delimiter character.fieldQuoter
- Quote character used to enclose fields.java.lang.NullPointerException
- if the input String reference is null.public DelimitedTextParser(java.lang.String lineIn, char fieldDelimiter, char fieldQuoter) throws java.lang.IllegalArgumentException
lineIn
- String to be parsed.fieldDelimiter
- Delimiter character.fieldQuoter
- Quote character used to enclose fields.java.lang.IllegalArgumentException
- if an error occurredjava.lang.NullPointerException
- if the input String reference is null.public void setParseString(java.lang.String lineIn) throws java.lang.IllegalArgumentException
lineIn
- String to be parsed.java.lang.IllegalArgumentException
- if an error occurredjava.lang.NullPointerException
- if the input String reference is null.public java.lang.String getParseString()
public void setQuoterEnabled(boolean quoterEnabled)
quoterEnabled
- Set to true for enabled or false for disabled.public void resetParseDefaults()
public void setParseDelimiter(char delimiter)
delimiter
- the field delimiter character.public char getParseDelimiter()
public void setParseQuoter(char quoter)
quoter
- character that encloses input fields.public char getParseQuoter()
public java.lang.String nextField() throws java.lang.IllegalArgumentException, java.lang.IllegalStateException
java.lang.IllegalArgumentException
- thrown if the string to be parsed is not
correctly formatted (for example, unmatched quote characters)
or if nextField invoked again after reaching the end of the
line.java.lang.IllegalStateException
- if an error occurredpublic java.util.List<java.lang.String> parseAll() throws java.lang.IllegalArgumentException, java.lang.IllegalStateException
List<String>
of fields parsed from the input String.java.lang.IllegalArgumentException
- thrown if the string to be parsed is not
correctly formatted (for example, unmatched quote characters)
or if parseAll invoked again after reaching the end of the
line.java.lang.IllegalStateException
- if an error occurredpublic static void main(java.lang.String[] args)
args
- not used.