Class TextFileParser
TextFileParser
class allows easy parsing of text files.
If the specified file ends with .zip, then it the first one in the archive is automatically unzipped.
In the text file to parse, each line may contain at most one value.
The recognised datatypes are:
String
(unquoted)char
byte
int
double
boolean
(yes, true, 1, no, false and 0)DateStamp
(dd/MM/yyyy)TimeStamp
(HH:mm:ss or HH:mm:ss.mls)- comma-separated values (CSV)
Note that this class cannot be subclassed!
- Version:
- 04/04/2020
- Author:
- Sven Maerivoet
-
Constructor Summary
ConstructorDescriptionTextFileParser
(InputStream inputStream) Sets up a text file parser for the specifiedInputStream
.TextFileParser
(InputStream inputStream, String encoding) Sets up a text file parser for the specifiedInputStream
.TextFileParser
(String filename) Sets up a text file parser for the specified file.TextFileParser
(String filename, String encoding) Sets up a text file parser for the specified file. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns whether or not the parsing has reached the end of the file.Returns all the lines as aStringBuilder
object.String[]
Returns all the lines as aString[]
array.int
Returns the number of the line that the parser last tried to read.boolean
Returns the next line converted to aboolean
(empty lines are ignored).byte
Returns the next line converted to abyte
(empty lines are ignored).char
Returns the next line converted to achar
(empty lines are ignored).String[]
Returns the next line converted to aString[]
array of comma-separated values.String[]
getNextCSV
(char splitChar) Returns the next line converted to aString[]
array of comma-separated values with a specified split character.Returns the next line converted to aDateStamp
(empty lines are ignored).void
getNextDateTimeStamp
(DateStamp date, TimeStamp time) Convert the next line converted to aDateStamp
andTimeStamp
(empty lines are ignored).double
Returns the next line converted to adouble
(empty lines are ignored).int
Returns the next line converted to anint
(empty lines are ignored).Returns the next non-empty line converted to aString
.Returns the next line converted to aString
.Returns the next line converted to aTimeStamp
(empty lines are ignored).loadDoubleCSVFile
(String filename) Loads a file containing a table with comma-separateddouble
s (empty lines are ignored).static double[]
loadDoubleFile
(String filename) Loads a file containing a single column ofdouble
s (empty lines are ignored).loadGeneralCSVFile
(String filename, boolean ignoreComments) Loads a file containing comma-separated values (empty lines are ignored).
-
Constructor Details
-
TextFileParser
Sets up a text file parser for the specified file.- Parameters:
filename
- the name of the file to parse- Throws:
FileNotFoundException
- if the file is not found
-
TextFileParser
Sets up a text file parser for the specified file.- Parameters:
filename
- the name of the file to parseencoding
- the encoding used (e.g., UTF-8)- Throws:
FileNotFoundException
- if the file is not found
-
TextFileParser
Sets up a text file parser for the specifiedInputStream
.Note that a buffer is automatically wrapped around the specified
InputStream
.- Parameters:
inputStream
- theInputStream
containing the contents to parse
-
TextFileParser
Sets up a text file parser for the specifiedInputStream
.Note that a buffer is automatically wrapped around the specified
InputStream
.- Parameters:
encoding
- the encoding used (e.g., UTF-8)inputStream
- theInputStream
containing the contents to parse
-
-
Method Details
-
endOfFileReached
public boolean endOfFileReached()Returns whether or not the parsing has reached the end of the file.- Returns:
true
if the end of the file is reached,false
otherwise
-
getLastReadLineNr
public int getLastReadLineNr()Returns the number of the line that the parser last tried to read.- Returns:
- the number of the line that the parser last tried to read
-
getNextString
Returns the next line converted to aString
.Note that any whitespace surrounding the
String
is automatically trimmed.- Returns:
- the next line converted to a
String
- Throws:
FileParseException
- if the end-of-file is reached (the exception only contains the line number)
-
getNextNonEmptyString
Returns the next non-empty line converted to aString
.Note that any whitespace surrounding the
String
is automatically trimmed.- Returns:
- the next non-empty line converted to a
String
- Throws:
FileParseException
- if the end-of-file is reached (the exception only contains the line number)
-
getNextChar
Returns the next line converted to achar
(empty lines are ignored).- Returns:
- the next line converted to a
char
- Throws:
FileParseException
- if the end-of-file is reached (the exception only contains the line number)- See Also:
-
getNextByte
Returns the next line converted to abyte
(empty lines are ignored).- Returns:
- the next line converted to a
byte
- Throws:
FileParseException
- if the end-of-file is reached or the line contains a malformed byte (the exception contains the value and line number)- See Also:
-
getNextInteger
Returns the next line converted to anint
(empty lines are ignored).- Returns:
- the next line converted to an
int
- Throws:
FileParseException
- if the end-of-file is reached or the line contains a malformed integer (the exception contains the value and line number)- See Also:
-
getNextDouble
Returns the next line converted to adouble
(empty lines are ignored).- Returns:
- the next line converted to a
double
- Throws:
FileParseException
- if the end-of-file is reached or the line contains a malformed double (the exception contains the value and line number)- See Also:
-
getNextBoolean
Returns the next line converted to aboolean
(empty lines are ignored).The following truth values are recognised:
- yes / true / 1
- no / false / 0
- Returns:
- the next line converted to a
boolean
- Throws:
FileParseException
- if the end-of-file is reached or the line contains a malformed boolean (the exception contains the value and line number)- See Also:
-
getNextDateStamp
Returns the next line converted to aDateStamp
(empty lines are ignored).The string has to have the following specific format:
dd/MM/yyyy, e.g., 11/04/1976
- Returns:
- the next line converted to a
DateStamp
- Throws:
FileParseException
- if the end-of-file is reached- See Also:
-
getNextTimeStamp
Returns the next line converted to aTimeStamp
(empty lines are ignored).The string has to have the following specific format: HH:mm:ss
- Returns:
- the next line converted to a
TimeStamp
- Throws:
FileParseException
- if the end-of-file is reached- See Also:
-
getNextDateTimeStamp
Convert the next line converted to aDateStamp
andTimeStamp
(empty lines are ignored). Any time zone information is ignored.The results are returned through the function's parameters.
The string has to have the following specific format: yyyy-MM-ddT:HH:mm:ssZ
- Parameters:
date
- theDateStamp
object to store the read date intime
- theTimeStamp
object to store the read time in- Throws:
FileParseException
- if the end-of-file is reached or the specified parameters arenull
- See Also:
-
getNextCSV
Returns the next line converted to aString[]
array of comma-separated values.The CSV parser can operate on quoted, unquoted and empty
String
s.- Returns:
- the next line converted to a
String[]
array of comma-separated values - Throws:
FileParseException
- if the end-of-file is reached (the exception only contains the line number)
-
getNextCSV
Returns the next line converted to aString[]
array of comma-separated values with a specified split character.The CSV parser can operate on quoted, unquoted and empty
String
s.- Parameters:
splitChar
- the character used to split the CSV record- Returns:
- the next line converted to a
String[]
array of comma-separated values - Throws:
FileParseException
- if the end-of-file is reached (the exception only contains the line number)
-
getAllLines
Returns all the lines as aStringBuilder
object.Note that any whitespace surrounding the lines is automatically trimmed.
- Returns:
- all the lines converted to a
StringBuilder
object - See Also:
-
getAllLinesAsStringArray
Returns all the lines as aString[]
array.Note that any whitespace surrounding the lines is automatically trimmed.
- Returns:
- all the lines converted to a
String[]
array - See Also:
-
loadGeneralCSVFile
Loads a file containing comma-separated values (empty lines are ignored).Commented lines are preceeded by a hash-tag (#).
- Parameters:
filename
- the name of the CSV file to loadignoreComments
- aboolean
indicating whether or not comments should be ignored- Returns:
- a sequence of CSV values,
null
is a problem occurred
-
loadDoubleFile
Loads a file containing a single column ofdouble
s (empty lines are ignored).- Parameters:
filename
- the name of the file to load- Returns:
- a sequence of
double
s,null
is a problem occurred
-
loadDoubleCSVFile
Loads a file containing a table with comma-separateddouble
s (empty lines are ignored).Commented lines preceeded by a hash-tag (#) are automatically ignored.
- Parameters:
filename
- the name of the file to load- Returns:
- a sequence of
double
s - Throws:
FileParseException
- if a malformed number was encountered
-