Class CsvBeanWriter

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, ICsvBeanWriter, ICsvWriter

public class CsvBeanWriter extends AbstractCsvWriter implements ICsvBeanWriter
CsvBeanWriter writes a CSV file by mapping each field on the bean to a column in the CSV file (using the supplied name mapping).
  • Field Details

    • beanValues

      private final List<Object> beanValues
    • processedColumns

      private final List<Object> processedColumns
    • cache

      private final MethodCache cache
  • Constructor Details

    • CsvBeanWriter

      public CsvBeanWriter(Writer writer, CsvPreference preference)
      Constructs a new CsvBeanWriter with the supplied Writer and CSV preferences. Note that the writer will be wrapped in a BufferedWriter before accessed.
      Parameters:
      writer - the writer
      preference - the CSV preferences
      Throws:
      NullPointerException - if writer or preference are null
  • Method Details

    • extractBeanValues

      private void extractBeanValues(Object source, String[] nameMapping)
      Extracts the bean values, using the supplied name mapping array.
      Parameters:
      source - the bean
      nameMapping - the name mapping
      Throws:
      NullPointerException - if source or nameMapping are null
      SuperCsvReflectionException - if there was a reflection exception extracting the bean value
    • write

      public void write(Object source, String... nameMapping) throws IOException
      Writes the fields of the object as columns of a CSV file, using the supplied name mapping to map fields to the appropriate columns. toString() will be called on each element prior to writing.
      Specified by:
      write in interface ICsvBeanWriter
      Parameters:
      source - the object (bean instance) containing the values to write
      nameMapping - an array of Strings linking the fields in the bean to their corresponding CSV columns (the array length should match the number of columns). A null entry in the array indicates that the column should be ignored (the column will be empty).
      Throws:
      IOException - if an I/O error occurred
    • write

      public void write(Object source, String[] nameMapping, CellProcessor[] processors) throws IOException
      Writes the fields of the object as columns of a CSV file, using the supplied name mapping to map fields to the appropriate columns. toString() will be called on each (processed) element prior to writing.
      Specified by:
      write in interface ICsvBeanWriter
      Parameters:
      source - the object (bean instance) containing the values to write
      nameMapping - an array of Strings linking the fields in the bean to their corresponding CSV columns (the array length should match the number of columns). A null entry in the array indicates that the column should be ignored (the column will be empty).
      processors - an array of CellProcessors used to further process data before it is written (each element in the processors array corresponds with a CSV column - the number of processors should match the number of columns). A null entry indicates no further processing is required (the value returned by toString() will be written as the column value).
      Throws:
      IOException - if an I/O error occurred