Class CSVWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    SyncScheduleWriter

    public class CSVWriter
    extends java.lang.Object
    implements java.io.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 Summary

      Constructors 
      Constructor Description
      CSVWriter​(java.io.File file, java.lang.String encoding)
      Creates a new CSVWriter with the given File and encoding.
      CSVWriter​(java.io.File file, java.lang.String encoding, boolean append)
      Creates a new CSVWriter with the given File and encoding.
      CSVWriter​(java.io.OutputStream os, java.lang.String encoding)
      Default Constructor.
      CSVWriter​(java.io.Writer writer)
      Creates a new CSVWriter with the given writer.
      CSVWriter​(java.lang.String fileName, java.lang.String encoding)
      Creates a new CSVWriter with the given file name and encoding.
      CSVWriter​(java.lang.String fileName, java.lang.String encoding, boolean append)
      Creates a new CSVWriter with the given file name and encoding.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the writer.
      void closeQuietly()
      Close the writer quietly.
      java.lang.String createCSVLine​(java.util.Map fields)
      Creates a valid CSV-data line from given field map.
      char getCommentchar()
      Gets used char for commenting a line.
      protected char getDefaultCommentChar()  
      protected char getDefaultFieldSeparator()  
      protected java.lang.String getDefaultLineBreak()  
      protected java.lang.String[] getDefaultLineSeparators()  
      protected char getDefaultTextSeparator()  
      char getFieldseparator()
      Gets used char for separating fields.
      java.lang.String getLinebreak()
      Gets used text for separating lines.
      char getTextseparator()
      Gets used char for enclosing fields if they contain field separators.
      java.io.Writer getWriter()
      Gets the underlying writer instance.
      void setCommentchar​(char commentchar)
      Sets the character for commenting a line.
      void setFieldseparator​(char fieldseparator)
      Sets the character for separating fields.
      void setLinebreak​(java.lang.String linebreak)
      Sets the text for separating lines.
      void setTextseparator​(char textseparator)
      Sets the character for enclosing a field.
      int write​(java.util.List data)
      Writes the given List to the writer.
      void write​(java.util.Map linedata)
      Writes a single line to the writer.
      void writeComment​(java.lang.String scrline)
      Writes the given line as a comment line to the writer/file.
      void writeSrcLine​(java.lang.String scrline)
      The given line will be written to the writer without modification to the line.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CSVWriter

        public CSVWriter​(java.io.File file,
                         java.lang.String encoding)
                  throws java.io.UnsupportedEncodingException,
                         java.io.FileNotFoundException
        Creates a new CSVWriter with the given File and encoding. Default encoding is
        Parameters:
        file - the file
        encoding - the encoding
        Throws:
        java.io.UnsupportedEncodingException - if false encoding
        java.io.FileNotFoundException - if file not found
      • CSVWriter

        public CSVWriter​(java.io.File file,
                         java.lang.String encoding,
                         boolean append)
                  throws java.io.UnsupportedEncodingException,
                         java.io.FileNotFoundException
        Creates a new CSVWriter with the given File and encoding. Default encoding is CSVConstants.DEFAULT_ENCODING.
        Parameters:
        file - the file
        encoding - the encoding
        append - write to file in append mode (written data will be added to the existing one)?
        Throws:
        java.io.UnsupportedEncodingException - if false encoding
        java.io.FileNotFoundException - if file not found
      • CSVWriter

        public CSVWriter​(java.io.OutputStream os,
                         java.lang.String encoding)
                  throws java.io.UnsupportedEncodingException
        Default Constructor. Creates a new CSVWriter with the given output stream and encoding. Default encoding is CSVConstants.DEFAULT_ENCODING.
        Parameters:
        os - the outputstream
        encoding - the encoding
        Throws:
        java.io.UnsupportedEncodingException - if false encoding
      • CSVWriter

        public CSVWriter​(java.lang.String fileName,
                         java.lang.String encoding)
                  throws java.io.UnsupportedEncodingException,
                         java.io.FileNotFoundException
        Creates a new CSVWriter with the given file name and encoding. Default encoding is CSVConstants.DEFAULT_ENCODING.
        Parameters:
        fileName - the file name
        encoding - the encoding
        Throws:
        java.io.UnsupportedEncodingException - if false encoding
        java.io.FileNotFoundException - if file not found
      • CSVWriter

        public CSVWriter​(java.lang.String fileName,
                         java.lang.String encoding,
                         boolean append)
                  throws java.io.UnsupportedEncodingException,
                         java.io.FileNotFoundException
        Creates a new CSVWriter with the given file name and encoding. Default encoding is CSVConstants.DEFAULT_ENCODING.
        Parameters:
        fileName - the file name
        encoding - the encoding
        append - write to file in append mode (written data will be added to the existing one)?
        Throws:
        java.io.UnsupportedEncodingException - if false encoding
        java.io.FileNotFoundException - if file not found
      • CSVWriter

        public CSVWriter​(java.io.Writer writer)
        Creates a new CSVWriter with the given writer.
        Parameters:
        writer - the writer
    • Method Detail

      • getDefaultCommentChar

        protected char getDefaultCommentChar()
      • getDefaultFieldSeparator

        protected char getDefaultFieldSeparator()
      • getDefaultTextSeparator

        protected char getDefaultTextSeparator()
      • getDefaultLineBreak

        protected java.lang.String getDefaultLineBreak()
      • getDefaultLineSeparators

        protected java.lang.String[] getDefaultLineSeparators()
      • close

        public void close()
                   throws java.io.IOException
        Closes the writer.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.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 java.lang.String createCSVLine​(java.util.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 java.lang.String getLinebreak()
        Gets used text for separating lines.
        Returns:
        used text for separating lines
      • getWriter

        public java.io.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​(java.lang.String linebreak)
        Sets the text for separating lines.
        Parameters:
        linebreak - text for separating lines
      • write

        public int write​(java.util.List data)
                  throws java.io.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:
        java.io.IOException - error while writing
      • write

        public void write​(java.util.Map linedata)
                   throws java.io.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:
        java.io.IOException - error while writing
      • writeComment

        public void writeComment​(java.lang.String scrline)
                          throws java.io.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:
        java.io.IOException - error while writing
      • writeSrcLine

        public void writeSrcLine​(java.lang.String scrline)
                          throws java.io.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:
        java.io.IOException - error while writing