Class TextFileParser

java.lang.Object
org.sm.smtools.util.TextFileParser

public final class TextFileParser
extends java.lang.Object
The 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 specified InputStream.
    TextFileParser​(java.io.InputStream inputStream, java.lang.String encoding)
    Sets up a text file parser for the specified InputStream.
    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 a StringBuilder object.
    java.lang.String[] getAllLinesAsStringArray()
    Returns all the lines as a String[] array.
    int getLastReadLineNr()
    Returns the number of the line that the parser last tried to read.
    boolean getNextBoolean()
    Returns the next line converted to a boolean (empty lines are ignored).
    byte getNextByte()
    Returns the next line converted to a byte (empty lines are ignored).
    char getNextChar()
    Returns the next line converted to a char (empty lines are ignored).
    java.lang.String[] getNextCSV()
    Returns the next line converted to a String[] array of comma-separated values.
    java.lang.String[] getNextCSV​(char splitChar)
    Returns the next line converted to a String[] array of comma-separated values with a specified split character.
    DateStamp getNextDateStamp()
    Returns the next line converted to a DateStamp (empty lines are ignored).
    void getNextDateTimeStamp​(DateStamp date, TimeStamp time)
    Convert the next line converted to a DateStamp and TimeStamp (empty lines are ignored).
    double getNextDouble()
    Returns the next line converted to a double (empty lines are ignored).
    int getNextInteger()
    Returns the next line converted to an int (empty lines are ignored).
    java.lang.String getNextNonEmptyString()
    Returns the next non-empty line converted to a String.
    java.lang.String getNextString()
    Returns the next line converted to a String.
    TimeStamp getNextTimeStamp()
    Returns the next line converted to a TimeStamp (empty lines are ignored).
    static java.util.ArrayList<java.lang.Double[]> loadDoubleCSVFile​(java.lang.String filename)
    Loads a file containing a table with comma-separated doubles (empty lines are ignored).
    static double[] loadDoubleFile​(java.lang.String filename)
    Loads a file containing a single column of doubles (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.FileNotFoundException
      Sets 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.FileNotFoundException
      Sets up a text file parser for the specified file.
      Parameters:
      filename - the name of the file to parse
      encoding - 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 specified InputStream.

      Note that a buffer is automatically wrapped around the specified InputStream.

      Parameters:
      inputStream - the InputStream containing the contents to parse
    • TextFileParser

      public TextFileParser​(java.io.InputStream inputStream, java.lang.String encoding)
      Sets up a text file parser for the specified InputStream.

      Note that a buffer is automatically wrapped around the specified InputStream.

      Parameters:
      encoding - the encoding used (e.g., UTF-8)
      inputStream - the InputStream 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

      public java.lang.String getNextString() throws FileParseException
      Returns the next line converted to a String.

      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

      public java.lang.String getNextNonEmptyString() throws FileParseException
      Returns the next non-empty line converted to a String.

      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

      public char getNextChar() throws FileParseException
      Returns the next line converted to a char (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

      public byte getNextByte() throws FileParseException
      Returns the next line converted to a byte (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

      public int getNextInteger() throws FileParseException
      Returns the next line converted to an int (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

      public double getNextDouble() throws FileParseException
      Returns the next line converted to a double (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

      public boolean getNextBoolean() throws FileParseException
      Returns the next line converted to a boolean (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

      public DateStamp getNextDateStamp() throws FileParseException
      Returns the next line converted to a DateStamp (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

      public TimeStamp getNextTimeStamp() throws FileParseException
      Returns the next line converted to a TimeStamp (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

      public void getNextDateTimeStamp​(DateStamp date, TimeStamp time) throws FileParseException
      Convert the next line converted to a DateStamp and TimeStamp (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 - the DateStamp object to store the read date in
      time - the TimeStamp object to store the read time in
      Throws:
      FileParseException - if the end-of-file is reached or the specified parameters are null
      See Also:
      TimeStamp
    • getNextCSV

      public java.lang.String[] getNextCSV() throws FileParseException
      Returns the next line converted to a String[] array of comma-separated values.

      The CSV parser can operate on quoted, unquoted and empty Strings.

      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

      public java.lang.String[] getNextCSV​(char splitChar) throws FileParseException
      Returns the next line converted to a String[] array of comma-separated values with a specified split character.

      The CSV parser can operate on quoted, unquoted and empty Strings.

      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 a StringBuilder 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 a String[] 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 load
      ignoreComments - a boolean 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 of doubles (empty lines are ignored).
      Parameters:
      filename - the name of the file to load
      Returns:
      a sequence of doubles, null is a problem occurred
    • loadDoubleCSVFile

      public static java.util.ArrayList<java.lang.Double[]> loadDoubleCSVFile​(java.lang.String filename) throws FileParseException
      Loads a file containing a table with comma-separated doubles (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 doubles
      Throws:
      FileParseException - if a malformed number was encountered