Class CsvRecordWriter<T>

java.lang.Object
org.refcodes.tabular.CsvRecordWriter<T>
Type Parameters:
T - The type of the Record instances being used.
All Implemented Interfaces:
AutoCloseable, org.refcodes.io.RowWriter<T[]>, org.refcodes.mixin.DelimiterAccessor, org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<org.refcodes.textual.CsvMixin>, org.refcodes.mixin.DelimiterAccessor.DelimiterMutator, org.refcodes.mixin.DelimiterAccessor.DelimiterProperty, org.refcodes.mixin.TrimAccessor, org.refcodes.mixin.TrimAccessor.TrimBuilder<org.refcodes.textual.CsvMixin>, org.refcodes.mixin.TrimAccessor.TrimMutator, org.refcodes.mixin.TrimAccessor.TrimProperty, HeaderAccessor<T>, RecordWriter<T>, org.refcodes.textual.CsvEscapeModeAccessor, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeBuilder<org.refcodes.textual.CsvMixin>, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeMutator, org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeProperty, org.refcodes.textual.CsvMixin
Direct Known Subclasses:
CsvStringRecordWriter

public class CsvRecordWriter<T>
extends Object
implements org.refcodes.textual.CsvMixin, RecordWriter<T>
The CsvRecordWriter writes CSV-Data.
  • Field Details

    • _header

      protected Header<T> _header
    • _outputStream

      protected PrintStream _outputStream
    • _csvBuilder

      protected org.refcodes.textual.CsvBuilder _csvBuilder
  • Constructor Details

    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, File aFile) throws FileNotFoundException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      Throws:
      FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, File aFile, char aCsvDelimiter) throws FileNotFoundException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
    • CsvRecordWriter

      public CsvRecordWriter​(File aFile) throws FileNotFoundException
      Constructs a CsvRecordWriter.
      Parameters:
      aFile - The File to be used for printing output.
      Throws:
      FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
    • CsvRecordWriter

      public CsvRecordWriter​(File aFile, char aCsvDelimiter) throws FileNotFoundException
      Constructs a CsvRecordWriter.
      Parameters:
      aFile - The File to be used for printing output. aCsvDelimiter The separator to be used when separating the CSV values in the log output.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, OutputStream aOutputStream)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, OutputStream aOutputStream, char aCsvDelimiter)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
    • CsvRecordWriter

      public CsvRecordWriter​(OutputStream aOutputStream)
      Constructs a CsvRecordWriter.
      Parameters:
      aOutputStream - The OutputStream to be used for printing output.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, File aFile) throws FileNotFoundException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      Throws:
      FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, File aFile, char aCsvDelimiter) throws FileNotFoundException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, OutputStream aOutputStream)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, OutputStream aOutputStream, char aCsvDelimiter)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, File aFile, Charset aEncoding) throws IOException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      Throws:
      IOException - in case the file is not found or the encoding is not supported.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, File aFile, Charset aEncoding, char aCsvDelimiter) throws IOException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      IOException - in case the file is not found or the encoding is not supported.
    • CsvRecordWriter

      public CsvRecordWriter​(File aFile, Charset aEncoding) throws IOException
      Constructs a CsvRecordWriter.
      Parameters:
      aFile - The File to be used for printing output.
      Throws:
      IOException - in case the file is not found or the encoding is not supported.
    • CsvRecordWriter

      public CsvRecordWriter​(File aFile, Charset aEncoding, char aCsvDelimiter) throws IOException
      Constructs a CsvRecordWriter.
      Parameters:
      aFile - The File to be used for printing output. aCsvDelimiter The separator to be used when separating the CSV values in the log output.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      IOException - in case the file is not found or the encoding is not supported.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, OutputStream aOutputStream, Charset aEncoding) throws UnsupportedEncodingException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      Throws:
      UnsupportedEncodingException - thrown in case an unsupported encoding (Charset) has been provided.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, OutputStream aOutputStream, Charset aEncoding, char aCsvDelimiter) throws UnsupportedEncodingException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      UnsupportedEncodingException - thrown in case an unsupported encoding (Charset) has been provided.
    • CsvRecordWriter

      public CsvRecordWriter​(OutputStream aOutputStream, Charset aEncoding) throws UnsupportedEncodingException
      Constructs a CsvRecordWriter.
      Parameters:
      aOutputStream - The OutputStream to be used for printing output.
      Throws:
      UnsupportedEncodingException - thrown in case an unsupported encoding (Charset) has been provided.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, File aFile, Charset aEncoding) throws IOException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      Throws:
      IOException - in case the file is not found or the encoding is not supported.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, File aFile, Charset aEncoding, char aCsvDelimiter) throws IOException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aFile - The File to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      IOException - in case the file is not found or the encoding is not supported.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, OutputStream aOutputStream, Charset aEncoding) throws UnsupportedEncodingException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      Throws:
      UnsupportedEncodingException - thrown in case an unsupported encoding (Charset) has been provided.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, OutputStream aOutputStream, Charset aEncoding, char aCsvDelimiter) throws UnsupportedEncodingException
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aOutputStream - The OutputStream to be used for printing.
      aEncoding - The Charset for the character encoding to use.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
      Throws:
      UnsupportedEncodingException - thrown in case an unsupported encoding (Charset) has been provided.
    • CsvRecordWriter

      public CsvRecordWriter​(PrintStream aPrintStream)
      Constructs a CsvRecordWriter.
      Parameters:
      aPrintStream - The PrintStream to be used for printing.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, PrintStream aPrintStream)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - The header used for logging in the correct format.
      aPrintStream - The PrintStream to be used for printing.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, PrintStream aPrintStream, char aCsvDelimiter)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aPrintStream - The PrintStream to be used for printing.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
    • CsvRecordWriter

      public CsvRecordWriter​(Header<T> aHeader, PrintStream aPrintStream)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aPrintStream - The PrintStream to be used for printing.
    • CsvRecordWriter

      public CsvRecordWriter​(ColumnFactory<T> aColumnFactory, PrintStream aPrintStream, char aCsvDelimiter)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
      aPrintStream - The PrintStream to be used for printing output.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
    • CsvRecordWriter

      protected CsvRecordWriter​(Header<T> aHeader, ColumnFactory<T> aColumnFactory, PrintStream aPrintStream, char aCsvDelimiter)
      Constructs a CsvRecordWriter with the given header for logging.
      Parameters:
      aHeader - The header used for logging in the correct format.
      aColumnFactory - A ColumnFactory to be used to generate Column instances from the top line of the CSF file, required for parsing the CSV lines and converting them to Record instances.
      aPrintStream - The PrintStream to be used for printing output.
      aCsvDelimiter - The separator to be used when separating the CSV values in the log output.
  • Method Details

    • setCommentPrefixes

      public void setCommentPrefixes​(String... aCommentPrefixes)
      Specified by:
      setCommentPrefixes in interface org.refcodes.textual.CsvMixin
    • getCommentPrefixes

      public String[] getCommentPrefixes()
      Specified by:
      getCommentPrefixes in interface org.refcodes.textual.CsvMixin
    • clearCommentPrefixes

      public void clearCommentPrefixes()
      Specified by:
      clearCommentPrefixes in interface org.refcodes.textual.CsvMixin
    • withCommentPrefixes

      public org.refcodes.textual.CsvMixin withCommentPrefixes​(String... aCommentPrefixes)
      Specified by:
      withCommentPrefixes in interface org.refcodes.textual.CsvMixin
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • getCsvEscapeMode

      public org.refcodes.textual.CsvEscapeMode getCsvEscapeMode()
      Specified by:
      getCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor
    • isTrim

      public boolean isTrim()
      Specified by:
      isTrim in interface org.refcodes.mixin.TrimAccessor
    • getDelimiter

      public char getDelimiter()
      Specified by:
      getDelimiter in interface org.refcodes.mixin.DelimiterAccessor
    • setTrim

      public void setTrim​(boolean isTrim)
      Specified by:
      setTrim in interface org.refcodes.mixin.TrimAccessor.TrimMutator
    • setDelimiter

      public void setDelimiter​(char aCsvDelimiter)
      Specified by:
      setDelimiter in interface org.refcodes.mixin.DelimiterAccessor.DelimiterMutator
    • withTrim

      public CsvRecordWriter<T> withTrim​(boolean isTrimRecords)
      Specified by:
      withTrim in interface org.refcodes.mixin.TrimAccessor.TrimBuilder<T>
    • withCsvEscapeMode

      public CsvRecordWriter<T> withCsvEscapeMode​(org.refcodes.textual.CsvEscapeMode aCsvEscapeMode)
      Specified by:
      withCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeBuilder<T>
    • withDelimiter

      public CsvRecordWriter<T> withDelimiter​(char aCsvDelimiter)
      Specified by:
      withDelimiter in interface org.refcodes.mixin.DelimiterAccessor.DelimiterBuilder<T>
    • setCsvEscapeMode

      public void setCsvEscapeMode​(org.refcodes.textual.CsvEscapeMode aCsvEscapeMode)
      Specified by:
      setCsvEscapeMode in interface org.refcodes.textual.CsvEscapeModeAccessor.CsvEscapeModeMutator
    • parseHeader

      public void parseHeader​(String... aHeader)
      Parses and sets the Header from the provided String. You must have provided a ColumnFactory for this operation to succeed.
      Specified by:
      parseHeader in interface RecordWriter<T>
      Parameters:
      aHeader - The header to be created.
    • writeHeader

      public void writeHeader​(String... aHeader)
      Parses and sets the Header from the provided String arguments and writes the CSV header to the (standard) output (be it a Stream or a File). You must have provided a ColumnFactory for this operation to succeed.
      Specified by:
      writeHeader in interface RecordWriter<T>
      Parameters:
      aHeader - The header to be created.
    • writeHeader

      public void writeHeader​(Header<T> aHeader)
      Sets and writes the provided Header to the (standard) output (be it a Stream or a File). .
      Specified by:
      writeHeader in interface RecordWriter<T>
      Parameters:
      aHeader - The header to be created.
    • writeHeader

      public void writeHeader()
      Writes the CSV header to the (standard) output (be it a Stream or a File). You must have provided a Header for this operation to succeed.
      Specified by:
      writeHeader in interface RecordWriter<T>
    • writeNext

      public void writeNext​(T... aRecord) throws IllegalArgumentException
      Directly logs the provided String instances to the (standard) output.
      Specified by:
      writeNext in interface RecordWriter<T>
      Specified by:
      writeNext in interface org.refcodes.io.RowWriter<T>
      Parameters:
      aRecord - The String instances to be logged.
      Throws:
      IllegalArgumentException - thrown in case the provided objects do not align with the header (as of HeaderAccessor.getHeader()).
    • writeNext

      public void writeNext​(Record<T> aRecord) throws ColumnMismatchException, HeaderMismatchException
      Directly logs the provided String instances to the (standard) output.
      Specified by:
      writeNext in interface RecordWriter<T>
      Parameters:
      aRecord - The String instances to be logged.
      Throws:
      ColumnMismatchException - Thrown in case a value was found in the e.g. in a Record of the wrong type than specified by a provided Column (of for example a Header).
      HeaderMismatchException - Thrown in case there is a mismatch between the given HeaderMismatchException and the Row, i.e. the index for the given key in the header may be out of index of the given row or the given key does not exist in the Header.
    • writeComment

      public void writeComment​(String aComment) throws IllegalStateException
      Writes a comment by using the fist defined comment prefix as of getCommentPrefixes() suffixed with a space (" ") followed by the comment (as of the CsvMixin.toComment(String)). If no prefix has been set, then an IllegalStateException will be thrown.
      Parameters:
      aComment - The comment to be written.
      Throws:
      IllegalStateException - in case no comment prefixes have been set via setCommentPrefixes(String...) or withCommentPrefixes(String...).
    • writeHeaderComment

      public void writeHeaderComment​(String... aHeader)
      Writes the given Header as of writeHeader(String...) as a comment as of writeComment(String).
      Parameters:
      aHeader - The Header to be written as a comment.
    • writeHeaderComment

      public void writeHeaderComment​(Header<T> aHeader)
      Writes the given Header as of writeHeader(Header) as a comment as of writeComment(String).
      Parameters:
      aHeader - The Header to be written as a comment.
    • writeHeaderComment

      public void writeHeaderComment()
      Writes the Header as of writeHeader() as a comment as of writeComment(String).
    • getHeader

      public Header<T> getHeader()
      Retrieves the Header property.
      Specified by:
      getHeader in interface HeaderAccessor<T>
      Returns:
      The Header being stored by this property.