Class CsvWriter<T>

java.lang.Object
org.simpleflatmapper.csv.CsvWriter<T>
Type Parameters:
T - the type of object to write

public class CsvWriter<T> extends Object
A CsvWriter allows the caller to write object of type T to an appendable in a specified format. See from(Class) to create one.

The DSL allows to create a CsvWriter easily. The CsvWriter will by default append the headers on the call to CsvWriter.CsvWriterDSL.to(Appendable) Because the DSL create a mapper it is better to cache the CsvWriter.CsvWriterDSL.
CsvWriter csvWriter = CsvWriter.from(MyObject.class).to(myWriter);
csvWriter.append(obj1).append(obj2);

You can deactivate that by calling CsvWriter.CsvWriterDSL.skipHeaders()
CsvWriter csvWriter = CsvWriter.from(MyObject.class).skipHeaders().to(myWriter);

You can also specified the property names.
CsvWriter csvWriter = CsvWriter.from(MyObject.class).columns("id", "name").to(myWriter);

Or add a property with a specified format
CsvWriter csvWriter = CsvWriter.from(MyObject.class).columns("date", new SimpleDateFormat("yyyyMMdd")).to(myWriter);

  • Method Details

    • append

      public CsvWriter<T> append(T value) throws IOException
      write the specified value to the underlying appendable.
      Parameters:
      value - the value to write
      Returns:
      the current writer
      Throws:
      IOException - If an I/O error occurs
    • from

      public static <T> CsvWriter.CsvWriterDSL<T> from(Class<T> type)
      Create a DSL on the specified type.
      Type Parameters:
      T - the type
      Parameters:
      type - the type of object to write
      Returns:
      a DSL on the specified type
    • from

      public static <T> CsvWriter.CsvWriterDSL<T> from(org.simpleflatmapper.util.TypeReference<T> typeReference)
      Create a DSL on the specified type.
      Type Parameters:
      T - the type
      Parameters:
      typeReference - the type of object to write
      Returns:
      a DSL on the specified type
    • from

      public static <T> CsvWriter.CsvWriterDSL<T> from(Type type)
      Create a DSL on the specified type.
      Type Parameters:
      T - the type
      Parameters:
      type - the type of object to write
      Returns:
      a DSL on the specified type