Class 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 Detail

      • _header

        protected Header<T> _header
      • _csvBuilder

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

      • CsvRecordWriter

        public CsvRecordWriter​(Header<T> aHeader,
                               File aCsvFile)
                        throws FileNotFoundException
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aCsvFile - 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 aCsvFile,
                               char aCsvDelimiter)
                        throws FileNotFoundException
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aCsvFile - 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 aCsvFile)
                        throws FileNotFoundException
        Constructs a CsvRecordWriter.
        Parameters:
        aCsvFile - 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 aCsvFile,
                               char aCsvDelimiter)
                        throws FileNotFoundException
        Constructs a CsvRecordWriter.
        Parameters:
        aCsvFile - 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 aCsvOutputStream)
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aCsvOutputStream - The OutputStream to be used for printing.
      • CsvRecordWriter

        public CsvRecordWriter​(Header<T> aHeader,
                               OutputStream aCsvOutputStream,
                               char aCsvDelimiter)
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aCsvOutputStream - 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​(ColumnFactory<T> aColumnFactory,
                               File aCsvFile)
                        throws FileNotFoundException
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aCsvFile - 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 aCsvFile,
                               char aCsvDelimiter)
                        throws FileNotFoundException
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aCsvFile - 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 aCsvOutputStream)
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aCsvOutputStream - The OutputStream to be used for printing.
      • CsvRecordWriter

        public CsvRecordWriter​(ColumnFactory<T> aColumnFactory,
                               OutputStream aCsvOutputStream,
                               char aCsvDelimiter)
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aCsvOutputStream - 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 aCsvFile,
                               Charset aEncoding)
                        throws IOException
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aHeader - The header used for logging in the correct format.
        aCsvFile - 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 aCsvFile,
                               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.
        aCsvFile - 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 aCsvFile,
                               Charset aEncoding)
                        throws IOException
        Constructs a CsvRecordWriter.
        Parameters:
        aCsvFile - The File to be used for printing output.
        aEncoding - the encoding
        Throws:
        IOException - in case the file is not found or the encoding is not supported.
      • CsvRecordWriter

        public CsvRecordWriter​(File aCsvFile,
                               Charset aEncoding,
                               char aCsvDelimiter)
                        throws IOException
        Constructs a CsvRecordWriter.
        Parameters:
        aCsvFile - The File to be used for printing output. aCsvDelimiter The separator to be used when separating the CSV values in the log output.
        aEncoding - the encoding
        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 aCsvOutputStream,
                               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.
        aCsvOutputStream - 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​(ColumnFactory<T> aColumnFactory,
                               File aCsvFile,
                               Charset aEncoding)
                        throws IOException
        Constructs a CsvRecordWriter with the given header for logging.
        Parameters:
        aColumnFactory - The header used for logging in the correct format.
        aCsvFile - 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 aCsvFile,
                               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.
        aCsvFile - 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,
                               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 Detail

      • 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 CsvRecordWriter<T> withCommentPrefixes​(String... aCommentPrefixes)
        Specified by:
        withCommentPrefixes in interface org.refcodes.textual.CsvMixin
      • 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 writer (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 writer (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 writer (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>