Class TextFileParser
public final class TextFileParser
extends java.lang.Object
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
Constructors Constructor Description TextFileParser(java.io.InputStream inputStream)
Sets up a text file parser for the specifiedInputStream
.TextFileParser(java.io.InputStream inputStream, java.lang.String encoding)
Sets up a text file parser for the specifiedInputStream
.TextFileParser(java.lang.String filename)
Sets up a text file parser for the specified file.TextFileParser(java.lang.String filename, java.lang.String encoding)
Sets up a text file parser for the specified file. -
Method Summary
Modifier and Type Method Description boolean
endOfFileReached()
Returns whether or not the parsing has reached the end of the file.java.lang.StringBuilder
getAllLines()
Returns all the lines as aStringBuilder
object.java.lang.String[]
getAllLinesAsStringArray()
Returns all the lines as aString[]
array.int
getLastReadLineNr()
Returns the number of the line that the parser last tried to read.boolean
getNextBoolean()
Returns the next line converted to aboolean
(empty lines are ignored).byte
getNextByte()
Returns the next line converted to abyte
(empty lines are ignored).char
getNextChar()
Returns the next line converted to achar
(empty lines are ignored).java.lang.String[]
getNextCSV()
Returns the next line converted to aString[]
array of comma-separated values.java.lang.String[]
getNextCSV(char splitChar)
Returns the next line converted to aString[]
array of comma-separated values with a specified split character.DateStamp
getNextDateStamp()
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
getNextDouble()
Returns the next line converted to adouble
(empty lines are ignored).int
getNextInteger()
Returns the next line converted to anint
(empty lines are ignored).java.lang.String
getNextNonEmptyString()
Returns the next non-empty line converted to aString
.java.lang.String
getNextString()
Returns the next line converted to aString
.TimeStamp
getNextTimeStamp()
Returns the next line converted to aTimeStamp
(empty lines are ignored).static java.util.ArrayList<java.lang.Double[]>
loadDoubleCSVFile(java.lang.String filename)
Loads a file containing a table with comma-separateddouble
s (empty lines are ignored).static double[]
loadDoubleFile(java.lang.String filename)
Loads a file containing a single column ofdouble
s (empty lines are ignored).static java.util.ArrayList<java.lang.String[]>
loadGeneralCSVFile(java.lang.String filename, boolean ignoreComments)
Loads a file containing comma-separated values (empty lines are ignored).Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
TextFileParser
public TextFileParser(java.lang.String filename) throws java.io.FileNotFoundExceptionSets up a text file parser for the specified file.- Parameters:
filename
- the name of the file to parse- Throws:
java.io.FileNotFoundException
- if the file is not found
-
TextFileParser
public TextFileParser(java.lang.String filename, java.lang.String encoding) throws java.io.FileNotFoundExceptionSets 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:
java.io.FileNotFoundException
- if the file is not found
-
TextFileParser
public TextFileParser(java.io.InputStream inputStream)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
public TextFileParser(java.io.InputStream inputStream, java.lang.String encoding)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:
getNextString()
-
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:
getNextString()
-
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:
getNextString()
-
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:
getNextString()
-
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:
getNextString()
-
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:
DateStamp
-
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:
TimeStamp
-
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:
TimeStamp
-
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
public java.lang.StringBuilder 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:
getNextString()
,getAllLinesAsStringArray()
-
getAllLinesAsStringArray
public java.lang.String[] 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:
getNextString()
,getAllLines()
-
loadGeneralCSVFile
public static java.util.ArrayList<java.lang.String[]> loadGeneralCSVFile(java.lang.String filename, boolean ignoreComments)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
public static double[] loadDoubleFile(java.lang.String filename)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
public static java.util.ArrayList<java.lang.Double[]> loadDoubleCSVFile(java.lang.String filename) throws FileParseExceptionLoads 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
-