Package cdc.util.csv

Class CsvWriter

  • All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

    public class CsvWriter
    extends Object
    implements Flushable, Closeable
    Utility used to write CSV files.

    Cell separator can be controlled.
    End of line can be controlled.
    Escaping end multi line cells are supported.

    Author:
    Damien Carbonne
    • Field Detail

      • DEFAULT_SEPARATOR

        public static final char DEFAULT_SEPARATOR
        The default separator that should be used.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CsvWriter

        public CsvWriter​(Writer writer)
        Create a CsvWriter from a writer.
        Parameters:
        writer - The writer.
      • CsvWriter

        public CsvWriter​(PrintStream s,
                         String charset)
        Creates a CSvWriter from a PrintStream.

        Buffering is used.

        Parameters:
        s - The PrintStream.
        charset - The charset name.
      • CsvWriter

        public CsvWriter​(PrintStream s)
        Creates a CSvWriter from a PrintStream.

        Buffering is used.

        Parameters:
        s - The PrintStream.
      • CsvWriter

        public CsvWriter​(String filename,
                         String charset,
                         boolean append)
                  throws IOException
        Creates a CSvWriter from a file name.

        Buffering is used.

        Parameters:
        filename - The file name.
        charset - The charset name.
        append - If true, then bytes will be written to the end of the file rather than the beginning.
        Throws:
        IOException - When an IO error occurs.
      • CsvWriter

        public CsvWriter​(File file,
                         String charset,
                         boolean append)
                  throws IOException
        Creates a CSvWriter from a file.

        Buffering is used.

        Parameters:
        file - The file.
        charset - The charset name.
        append - If true, then bytes will be written to the end of the file rather than the beginning.
        Throws:
        IOException - When an IO error occurs.
    • Method Detail

      • getSeparator

        public char getSeparator()
        Returns:
        The used cell separator.
      • setSeparator

        public CsvWriter setSeparator​(char sep)
        Sets the cell separator.
        Parameters:
        sep - The separator.
        Returns:
        This CsvWriter.
      • getLineSeparator

        public String getLineSeparator()
        Returns:
        The used line separator.
      • setLineSeparator

        public CsvWriter setLineSeparator​(String sep)
        Sets the line separator to use.
        Parameters:
        sep - The line separator.
        Returns:
        This CsvWriter.
      • writeln

        public CsvWriter writeln()
                          throws IOException
        Writes the line separator (moves to next line).
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(CharSequence value,
                               boolean forceEscape)
                        throws IOException
        Writes a CharSequence cell and controls its escaping.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        forceEscape - If true, cell is escaped. If false, cell is escaped if necessary.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(CharSequence value)
                        throws IOException
        Writes a CharSequence cell, escaping it if necessary.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(String value,
                               boolean forceEscape)
                        throws IOException
        Writes a String cell and controls its escaping.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        forceEscape - If true, cell is escaped. If false, cell is escaped if necessary.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(String value)
                        throws IOException
        Writes a String cell, escaping it if necessary.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(Collection<String> values,
                               boolean forceEscape)
                        throws IOException
        Writes a collection of String cells and controls their escaping.

        If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.

        Parameters:
        values - The cells.
        forceEscape - If true, all cells are escaped. If false, each cell is escaped if necessary.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(Collection<String> values)
                        throws IOException
        Writes a collection of String cells, escaping them if necessary.

        If necessary, a cell separator is first appended.
        A cell separator is inserted between consecutive cells.
        Cells are escaped if necessary.

        Parameters:
        values - The cells.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • writeln

        public CsvWriter writeln​(Collection<String> values,
                                 boolean forceEscape)
                          throws IOException
        Writes a collection of String cells, controlling their escaping, then moves to next line.

        If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.

        Parameters:
        values - The cells.
        forceEscape - If true, all cells are escaped. If false, each cell is escaped if necessary.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • writeln

        public CsvWriter writeln​(Collection<String> values)
                          throws IOException
        Writes a collection of cells, escaping them if necessary, then moves to next line.

        If necessary, a cell separator is first appended.
        A cell separator is inserted between consecutive cells.
        Cells are escaped if necessary.

        Parameters:
        values - The cells.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(boolean forceEscape,
                               String... values)
                        throws IOException
        Writes an array of String cells and controls their escaping.

        If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.

        Parameters:
        forceEscape - If true, all cells are escaped. If false, each cell is escaped if necessary.
        values - The cells.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(String... values)
                        throws IOException
        Writes an array of String cells, escaping them if necessary.

        If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.

        Parameters:
        values - The cells.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • writeln

        public CsvWriter writeln​(boolean forceEscape,
                                 String... values)
                          throws IOException
        Writes an array of String cells, controlling their escaping, then moves to next line.

        If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.

        Parameters:
        forceEscape - If true, all cells are escaped. If false, each cell is escaped if necessary.
        values - The cells.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • writeln

        public CsvWriter writeln​(String... values)
                          throws IOException
        Writes an array of String cells, escaping if necessary, then moves to next line.

        If necessary, a cell separator is first appended. A cell separator is inserted between consecutive cells.

        Parameters:
        values - The cells.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(boolean value)
                        throws IOException
        Writes a boolean cell.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(long value)
                        throws IOException
        Writes a long cell.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(int value)
                        throws IOException
        Writes an int cell.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(short value)
                        throws IOException
        Writes a short cell.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(byte value)
                        throws IOException
        Writes a byte cell.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(double value)
                        throws IOException
        Writes a double cell.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public CsvWriter write​(float value)
                        throws IOException
        Writes a float cell.

        If necessary, a cell separator is first appended.

        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.
      • write

        public <E extends Enum<E>> CsvWriter write​(E value)
                                            throws IOException
        Writes an enum cell.

        If necessary, a cell separator is first appended.

        Type Parameters:
        E - The enum type.
        Parameters:
        value - The cell.
        Returns:
        This CsvWriter.
        Throws:
        IOException - When an IO error occurs.