Class TextFileWriter

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

public final class TextFileWriter
extends java.lang.Object
The TextFileWriter class allows easy writing to text files.

In the resulting text file, each line will contain at most one value.

The available datatypes to write are:

  • int
  • double
  • String (unquoted)
  • boolean (true and false)
  • Date (dd/mm/yyyy)
  • Time (hh:mm:ss.ms)
  • comma-separated values (CSV)

Note that this class cannot be subclassed!

Version:
02/08/2019
Author:
Sven Maerivoet
  • Constructor Summary

    Constructors
    Constructor Description
    TextFileWriter​(java.lang.String filename)
    Sets up a text file writer for the specified file.
    TextFileWriter​(java.lang.String filename, boolean append)
    Sets up a text file writer for the specified file.
  • Method Summary

    Modifier and Type Method Description
    void close()
    Closes the file.
    void writeBoolean​(boolean b)
    Writes a string representation of a boolean to the file.
    void writeCSV​(java.lang.String[] csvValues)
    Write comma-separated values (CSV) to the file.
    void writeDate​(DateStamp dateStamp)
    Writes a string representation of a DateStamp object to the file.
    void writeDateTimeStamp​(DateStamp dateStamp, TimeStamp timeStamp)
    Writes a string representation of both a DateStamp and a TimeStamp object to the file.
    void writeDouble​(double d)
    Writes a string representation of a double to the file.
    void writeInteger​(int i)
    Writes a string representation of an int to the file.
    void writeLn()
    Writes an empty line to the file.
    void writeString​(java.lang.String s)
    Writes a string to the file.
    void writeTimeStamp​(TimeStamp timeStamp)
    Writes a string representation of a TimeStamp object to the file.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TextFileWriter

      public TextFileWriter​(java.lang.String filename) throws FileCantBeCreatedException
      Sets up a text file writer for the specified file.
      Parameters:
      filename - the name of the file to create
      Throws:
      FileCantBeCreatedException - if the file cannot be created
    • TextFileWriter

      public TextFileWriter​(java.lang.String filename, boolean append) throws FileCantBeCreatedException
      Sets up a text file writer for the specified file.
      Parameters:
      filename - the name of the file to create
      append - whether or not the (existing) file should be appended
      Throws:
      FileCantBeCreatedException - if the file cannot be created
  • Method Details

    • close

      public final void close()
      Closes the file.
    • writeLn

      public final void writeLn() throws FileWriteException
      Writes an empty line to the file.
      Throws:
      FileWriteException - if an error during the writing occurred
    • writeInteger

      public final void writeInteger​(int i) throws FileWriteException
      Writes a string representation of an int to the file.
      Parameters:
      i - the int to write to the file
      Throws:
      FileWriteException - if the int could not be written to the file
    • writeDouble

      public final void writeDouble​(double d) throws FileWriteException
      Writes a string representation of a double to the file.
      Parameters:
      d - the double to write to the file
      Throws:
      FileWriteException - if the double could not be written to the file
    • writeString

      public final void writeString​(java.lang.String s) throws FileWriteException
      Writes a string to the file.
      Parameters:
      s - the String to write to the file
      Throws:
      FileWriteException - if the String could not be written to the file
    • writeBoolean

      public final void writeBoolean​(boolean b) throws FileWriteException
      Writes a string representation of a boolean to the file.

      The string representation is either TRUE or FALSE.

      Parameters:
      b - the boolean to write to the file
      Throws:
      FileWriteException - if the boolean could not be written to the file
    • writeDate

      public final void writeDate​(DateStamp dateStamp) throws FileWriteException
      Writes a string representation of a DateStamp object to the file.

      The string representation of the DateStamp object is dd/mm/yyyy, e.g., "11/04/1976".

      Parameters:
      dateStamp - the DateStamp object to write to the file
      Throws:
      FileWriteException - if the DateStamp object could not be written to the file
    • writeTimeStamp

      public final void writeTimeStamp​(TimeStamp timeStamp) throws FileWriteException
      Writes a string representation of a TimeStamp object to the file.

      The string representation of the TimeStamp object is hh:mm:ss.ms, e.g., "12:45:16.154".

      Parameters:
      timeStamp - the TimeStamp object to write to the file
      Throws:
      FileWriteException - if the TimeStamp object could not be written to the file
    • writeDateTimeStamp

      public final void writeDateTimeStamp​(DateStamp dateStamp, TimeStamp timeStamp) throws FileWriteException
      Writes a string representation of both a DateStamp and a TimeStamp object to the file.

      The string representation of the TimeStamp object is yyyy-MM-ddT:HH:mm:ssZ.

      Parameters:
      dateStamp - the DateStamp object to write to the file
      timeStamp - the TimeStamp object to write to the file
      Throws:
      FileWriteException - if the TimeStamp object could not be written to the file
    • writeCSV

      public final void writeCSV​(java.lang.String[] csvValues) throws FileWriteException
      Write comma-separated values (CSV) to the file.

      Note that all strings are automatically quoted.

      Parameters:
      csvValues - the comma-separated values to write to the file
      Throws:
      FileWriteException - if the TimeStamp object could not be written to the file