Class CSVWriter

java.lang.Object
de.hybris.platform.util.CSVWriter
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
SyncScheduleWriter

public class CSVWriter extends Object implements Closeable
This class writes maps of text fields to a CSV OutputStream. Each map of text fields (a field is mapped to the desired position in line) represents a line which will be transformed to a valid CSV line and written to the stream. The format of the CSV-output is conform to RFC-4180 if using default settings.

Use of this CSVWriter:

  1. Create the Writer
    csvwriter = new CSVWriter(outputStream, null ); // null for default encoding
    Use the constructor to set the encoding and the stream. There are also other constructors available.
  2. Configure the Writer
    Example: csvwriter.setTextseparator('?');
    If you skip this step, following defaults are set:
    • commentOut = '#' //chars for comment lines
    • fieldseparator = ';' //separate the CSV fields
    • textseparator = '\"' //enclose a CSV text
    • linebreak = '\n' //separates lines
  3. Use the Writer
     Map line = new HashMap();
     line.add(new Integer(0), "firstField");
     line.add(new Integer(1), "secondField");
     csvwriter.write(line);
     csvwriter.writeComment("a comment line");
     
  4. Close the Writer
    Do not forget to call csvwriter.close() to close the writer. Without you can not access the destination file.
  • Constructor Details

  • Method Details

    • getDefaultCommentChar

      protected char getDefaultCommentChar()
    • getDefaultFieldSeparator

      protected char getDefaultFieldSeparator()
    • getDefaultTextSeparator

      protected char getDefaultTextSeparator()
    • getDefaultLineBreak

      protected String getDefaultLineBreak()
    • getDefaultLineSeparators

      protected String[] getDefaultLineSeparators()
    • close

      public void close() throws IOException
      Closes the writer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - error while closing
    • closeQuietly

      public void closeQuietly()
      Close the writer quietly. The IOException will be catched and if the debug mode is enabled the exeption message is written to the log.
    • createCSVLine

      public String createCSVLine(Map fields)
      Creates a valid CSV-data line from given field map.
      Parameters:
      fields - map with field texts mapped to the desired field position
      Returns:
      created CSV-line
    • getCommentchar

      public char getCommentchar()
      Gets used char for commenting a line.
      Returns:
      used char for commenting a line
    • getFieldseparator

      public char getFieldseparator()
      Gets used char for separating fields.
      Returns:
      used char for separating fields
    • getTextseparator

      public char getTextseparator()
      Gets used char for enclosing fields if they contain field separators.
      Returns:
      used char for enclosing fields if they contain field separators
    • getLinebreak

      public String getLinebreak()
      Gets used text for separating lines.
      Returns:
      used text for separating lines
    • getWriter

      public Writer getWriter()
      Gets the underlying writer instance.
      Returns:
      the underlying writer instance
    • setCommentchar

      public void setCommentchar(char commentchar)
      Sets the character for commenting a line.
      Parameters:
      commentchar - character for commenting
    • setFieldseparator

      public void setFieldseparator(char fieldseparator)
      Sets the character for separating fields.
      Parameters:
      fieldseparator - character for separating fields
    • setTextseparator

      public void setTextseparator(char textseparator)
      Sets the character for enclosing a field.
      Parameters:
      textseparator - character for enclosing field
    • setLinebreak

      public void setLinebreak(String linebreak)
      Sets the text for separating lines.
      Parameters:
      linebreak - text for separating lines
    • write

      public int write(List data) throws IOException
      Writes the given List to the writer. This method calls write(Map)
      Parameters:
      data - a List which contains Maps
      Returns:
      how many lines are written
      Throws:
      IOException - error while writing
    • write

      public void write(Map linedata) throws IOException
      Writes a single line to the writer. See CSVReader.parseLine(String) how the map looks like. The line will be quoted in the textseparator char.
      Map{
      { 0:Integer, Field_1:String },
      { 1:Integer, Field_2:String },
      ...
      { n:Integer, Field_n:String }
      }

      Parameters:
      linedata - the line which will be written
      Throws:
      IOException - error while writing
    • writeComment

      public void writeComment(String scrline) throws IOException
      Writes the given line as a comment line to the writer/file. Set the comment char with setCommentchar(char).
      Parameters:
      scrline - the comment line
      Throws:
      IOException - error while writing
    • writeSrcLine

      public void writeSrcLine(String scrline) throws IOException
      The given line will be written to the writer without modification to the line. At the end a '\n' is always written.
      Parameters:
      scrline - the line
      Throws:
      IOException - error while writing