Class CSVWriter

Object
CSVWriter
All Implemented Interfaces:
AutoCloseable

public class CSVWriter extends Object implements AutoCloseable
Writes a CSV file to the file-system.

It can be disabled, whereby no content is written to the file-system but method calls otherwise work as usual.

As it can be called by different threads, public methods are synchronized.

  • Method Details

    • createFromOutputterWithHeaders

      public static Optional<CSVWriter> createFromOutputterWithHeaders(String outputName, OutputterChecked outputter, Supplier<List<String>> headerNames, ErrorReporter errorReporter)
      Like createFromOutputter(String, OutputterChecked) but suppresses any exceptions into an error log - and writes headers.
      Parameters:
      outputName - unique name identifying the output which is used to construct a file-path to write to.
      outputter - how and whether outputs are written.
      headerNames - header-names for the CSV file
      errorReporter - used to reporter an error if the output cannot be created.
      Returns:
      the csv-writer if it's allowed, or empty if it's not, or if an error occurs.
    • createFromOutputter

      public static Optional<CSVWriter> createFromOutputter(String outputName, OutputterChecked outputter) throws OutputWriteFailedException
      Creates and starts a CSVWriter if it's allowed, otherwise returns Optional.empty().
      Parameters:
      outputName - unique name identifying the output which is used to construct a file-path to write to.
      outputter - how and whether outputs are written.
      Returns:
      the csv-writer if it's allowed, or empty if it's not.
      Throws:
      OutputWriteFailedException - if the CSV file cannot be created successfully.
    • create

      public static CSVWriter create(Path path) throws OutputWriteFailedException
      Creates and starts a CSVWriter (it's always allowed, so will never return null)
      Parameters:
      path - path to write the CSV to
      Returns:
      the csv-writer
      Throws:
      OutputWriteFailedException - if the CSV file cannot be created successfully.
    • hasWrittenHeaders

      public boolean hasWrittenHeaders()
      Whether the CSV file writes header-names as the first line.
      Returns:
      true iff the CSV file writes headers.
    • isOutputEnabled

      public boolean isOutputEnabled()
      Whether the output is enabled or not?

      When the output is disabled, no CSV file is written to the file-system, but the public methods work as usual.

      Returns:
      true iff the output is enabled.
    • writeHeaders

      public void writeHeaders(List<String> headerNames)
      Writes a line with the header-names of the columns.

      This is called zero or one times before calling writeRow(List) but the class does not enforce this order.

      It should not be called more than once.

      Parameters:
      headerNames - a name for each respective column. This should have an identical number of elements as elements in subsequent calls to writeRow(List).
    • writeRow

      public void writeRow(List<TypedValue> elements)
      Writes a line with values of a particular row

      This is always called after optionally calling writeHeaders(List) once but the class does not enforce this order.

      Parameters:
      elements - a value in the row for each respective column. This should have an identical number of elements each time it is called, and be equal to the number of elements in any previous call to writeHeaders(List).
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable