Class CSVPrinter

java.lang.Object
com.Ostermiller.util.CSVPrinter
All Implemented Interfaces:
CSVPrint

public class CSVPrinter extends Object implements CSVPrint
Print values as a comma separated list. More information about this class is available from ostermiller.org.
Since:
ostermillerutils 1.00.00
Author:
Stephen Ostermiller https://ostermiller.org/contact.pl?regarding=Java+Utilities, Pierre Dittgen <pierre dot dittgen at pass-tech dot fr>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a printer that will print values to the given stream.
    CSVPrinter(OutputStream out, char commentStart)
    Create a printer that will print values to the given stream.
    Create a printer that will print values to the given stream.
    CSVPrinter(Writer out, boolean alwaysQuote, boolean autoFlush)
    Create a printer that will print values to the given stream.
    CSVPrinter(Writer out, char commentStart)
    Create a printer that will print values to the given stream.
    CSVPrinter(Writer out, char commentStart, char quote, char delimiter)
    Create a printer that will print values to the given stream.
    CSVPrinter(Writer out, char commentStart, char quote, char delimiter, boolean alwaysQuote, boolean autoFlush)
    Create a printer that will print values to the given stream.
    CSVPrinter(Writer out, char commentStart, char quote, char delimiter, String lineEnding)
    Create a printer that will print values to the given stream.
    CSVPrinter(Writer out, char commentStart, char quote, char delimiter, String lineEnding, boolean alwaysQuote, boolean autoFlush)
    Create a printer that will print values to the given stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    changeDelimiter(char newDelimiter)
    Change this printer so that it uses a new delimiter.
    void
    changeQuote(char newQuote)
    Change this printer so that it uses a new character for quoting.
    boolean
    Flush the stream if it's not closed and check its error state.
    void
    Close any underlying streams.
    void
    Flush any data written out to underlying streams.
    void
    print(String value)
    Print the string as the next value on the line.
    void
    print(String[] values)
    Print a single line of comma separated values.
    void
    Output a blank line.
    void
    println(String value)
    Print the string as the last value on the line.
    void
    println(String[] values)
    Print a single line of comma separated values.
    void
    println(String[][] values)
    Print several lines of comma separated values.
    void
    Put a comment among the comma separated values.
    void
    setAlwaysQuote(boolean alwaysQuote)
    Set whether values printers should always be quoted, or whether the printer may, at its discretion, omit quotes around the value.
    void
    setAutoFlush(boolean autoFlush)
    Set flushing behavior.
    void
    setLineEnding(String lineEnding)
    Change this printer so that it uses a new line ending.
    void
    write(String value)
    Print the string as the next value on the line.
    void
    write(String[] values)
    Print a single line of comma separated values.
    void
    Output a blank line.
    void
    writeln(String value)
    Print the string as the last value on the line.
    void
    writeln(String[] values)
    Print a single line of comma separated values.
    void
    writeln(String[][] values)
    Print several lines of comma separated values.
    void
    Put a comment among the comma separated values.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CSVPrinter

      public CSVPrinter(OutputStream out)
      Create a printer that will print values to the given stream. Character to byte conversion is done using the default character encoding. Comments will be written using the default comment character '#', the delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.
      Parameters:
      out - stream to which to print.
      Since:
      ostermillerutils 1.00.00
    • CSVPrinter

      public CSVPrinter(Writer out)
      Create a printer that will print values to the given stream. Comments will be written using the default comment character '#', the delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.
      Parameters:
      out - stream to which to print.
      Since:
      ostermillerutils 1.00.00
    • CSVPrinter

      public CSVPrinter(OutputStream out, char commentStart)
      Create a printer that will print values to the given stream. Character to byte conversion is done using the default character encoding. The delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.
      Parameters:
      out - stream to which to print.
      commentStart - Character used to start comments.
      Since:
      ostermillerutils 1.00.00
    • CSVPrinter

      public CSVPrinter(Writer out, char commentStart)
      Create a printer that will print values to the given stream. The delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.
      Parameters:
      out - stream to which to print.
      commentStart - Character used to start comments.
      Since:
      ostermillerutils 1.00.00
    • CSVPrinter

      public CSVPrinter(Writer out, boolean alwaysQuote, boolean autoFlush)
      Create a printer that will print values to the given stream. The comment character will be the number sign, the delimiter will be the comma, the line ending will be the default system line ending, and the quote character will be double quotes.
      Parameters:
      out - stream to which to print.
      alwaysQuote - true if quotes should be used even when not strictly needed.
      autoFlush - should auto flushing be enabled.
      Since:
      ostermillerutils 1.02.26
    • CSVPrinter

      public CSVPrinter(Writer out, char commentStart, char quote, char delimiter) throws BadDelimiterException, BadQuoteException
      Create a printer that will print values to the given stream. The line ending will be the default system line ending, quotes will be used when needed, and auto flushing will be enabled.
      Parameters:
      out - stream to which to print.
      commentStart - Character used to start comments.
      quote - The new character to use for quoting.
      delimiter - The new delimiter character to use.
      Throws:
      BadQuoteException - if the character cannot be used as a quote.
      BadDelimiterException - if the character cannot be used as a delimiter.
      Since:
      ostermillerutils 1.02.26
    • CSVPrinter

      public CSVPrinter(Writer out, char commentStart, char quote, char delimiter, String lineEnding) throws BadDelimiterException, BadQuoteException, BadLineEndingException
      Create a printer that will print values to the given stream. Quotes will be used when needed, and auto flushing will be enabled.
      Parameters:
      out - stream to which to print.
      commentStart - Character used to start comments.
      quote - The new character to use for quoting.
      delimiter - The new delimiter character to use.
      lineEnding - The new line ending, or null to use the default line ending.
      Throws:
      BadQuoteException - if the character cannot be used as a quote.
      BadDelimiterException - if the character cannot be used as a delimiter.
      BadLineEndingException - if the line ending is not one of the three legal line endings.
      Since:
      ostermillerutils 1.06.01
    • CSVPrinter

      public CSVPrinter(Writer out, char commentStart, char quote, char delimiter, boolean alwaysQuote, boolean autoFlush) throws BadDelimiterException, BadQuoteException
      Create a printer that will print values to the given stream. The line ending will be the default system line ending,
      Parameters:
      out - stream to which to print.
      commentStart - Character used to start comments.
      quote - The new character to use for quoting.
      delimiter - The new delimiter character to use.
      alwaysQuote - true if quotes should be used even when not strictly needed.
      autoFlush - should auto flushing be enabled.
      Throws:
      BadQuoteException - if the character cannot be used as a quote.
      BadDelimiterException - if the character cannot be used as a delimiter.
      Since:
      ostermillerutils 1.02.26
    • CSVPrinter

      public CSVPrinter(Writer out, char commentStart, char quote, char delimiter, String lineEnding, boolean alwaysQuote, boolean autoFlush) throws BadDelimiterException, BadQuoteException, BadLineEndingException
      Create a printer that will print values to the given stream.
      Parameters:
      out - stream to which to print.
      commentStart - Character used to start comments.
      quote - The new character to use for quoting.
      delimiter - The new delimiter character to use.
      lineEnding - The new line ending, or null to use the default line ending.
      alwaysQuote - true if quotes should be used even when not strictly needed.
      autoFlush - should auto flushing be enabled.
      Throws:
      BadQuoteException - if the character cannot be used as a quote.
      BadDelimiterException - if the character cannot be used as a delimiter.
      BadLineEndingException - if the line ending is not one of the three legal line endings.
      Since:
      ostermillerutils 1.06.01
  • Method Details

    • changeDelimiter

      public void changeDelimiter(char newDelimiter) throws BadDelimiterException
      Change this printer so that it uses a new delimiter.
      Specified by:
      changeDelimiter in interface CSVPrint
      Parameters:
      newDelimiter - The new delimiter character to use.
      Throws:
      BadDelimiterException - if the character cannot be used as a delimiter.
      Since:
      ostermillerutils 1.02.18
    • changeQuote

      public void changeQuote(char newQuote) throws BadQuoteException
      Change this printer so that it uses a new character for quoting.
      Specified by:
      changeQuote in interface CSVPrint
      Parameters:
      newQuote - The new character to use for quoting.
      Throws:
      BadQuoteException - if the character cannot be used as a quote.
      Since:
      ostermillerutils 1.02.18
    • setLineEnding

      public void setLineEnding(String lineEnding) throws BadLineEndingException
      Change this printer so that it uses a new line ending.

      A line ending must be one of "\n", "\r", or "\r\n".

      The default line ending is the system line separator as specified by System.getProperty("line.separator"), or "\n" if the system line separator is not a legal line ending.

      Parameters:
      lineEnding - The new line ending, or null to use the default line ending.
      Throws:
      BadLineEndingException - if the line ending is not one of the three legal line endings.
      Since:
      ostermillerutils 1.06.01
    • println

      public void println(String value)
      Print the string as the last value on the line. The value will be quoted if needed.

      This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

      Specified by:
      println in interface CSVPrint
      Parameters:
      value - value to be outputted.
      Since:
      ostermillerutils 1.00.00
    • writeln

      public void writeln(String value) throws IOException
      Print the string as the last value on the line. The value will be quoted if needed.
      Specified by:
      writeln in interface CSVPrint
      Parameters:
      value - value to be outputted.
      Throws:
      IOException - if an error occurs while writing.
      Since:
      ostermillerutils 1.02.26
    • println

      public void println()
      Output a blank line.

      This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

      Specified by:
      println in interface CSVPrint
      Since:
      ostermillerutils 1.00.00
    • writeln

      public void writeln() throws IOException
      Output a blank line.
      Specified by:
      writeln in interface CSVPrint
      Throws:
      IOException - if an error occurs while writing.
      Since:
      ostermillerutils 1.02.26
    • println

      public void println(String[] values)
      Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.

      This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

      Specified by:
      println in interface CSVPrint
      Parameters:
      values - values to be outputted.
      Since:
      ostermillerutils 1.00.00
    • writeln

      public void writeln(String[] values) throws IOException
      Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.
      Specified by:
      writeln in interface CSVPrint
      Parameters:
      values - values to be outputted.
      Throws:
      IOException - if an error occurs while writing.
      Since:
      ostermillerutils 1.02.26
    • print

      public void print(String[] values)
      Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.

      This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

      Specified by:
      print in interface CSVPrint
      Parameters:
      values - values to be outputted.
      Since:
      ostermillerutils 1.00.00
    • write

      public void write(String[] values) throws IOException
      Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.
      Specified by:
      write in interface CSVPrint
      Parameters:
      values - values to be outputted.
      Throws:
      IOException - if an error occurs while writing.
      Since:
      ostermillerutils 1.02.26
    • println

      public void println(String[][] values)
      Print several lines of comma separated values. The values will be quoted if needed. Quotes and newLine characters will be escaped.

      This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

      Specified by:
      println in interface CSVPrint
      Parameters:
      values - values to be outputted.
      Since:
      ostermillerutils 1.00.00
    • writeln

      public void writeln(String[][] values) throws IOException
      Print several lines of comma separated values. The values will be quoted if needed. Quotes and newLine characters will be escaped.
      Specified by:
      writeln in interface CSVPrint
      Parameters:
      values - values to be outputted.
      Throws:
      IOException - if an error occurs while writing.
      Since:
      ostermillerutils 1.02.26
    • printlnComment

      public void printlnComment(String comment)
      Put a comment among the comma separated values. Comments will always begin on a new line and occupy a least one full line. The character specified to star comments and a space will be inserted at the beginning of each new line in the comment. If the comment is null, an empty comment is outputted.

      This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writelnComment method.

      Specified by:
      printlnComment in interface CSVPrint
      Parameters:
      comment - the comment to output.
      Since:
      ostermillerutils 1.00.00
    • writelnComment

      public void writelnComment(String comment) throws IOException
      Put a comment among the comma separated values. Comments will always begin on a new line and occupy a least one full line. The character specified to star comments and a space will be inserted at the beginning of each new line in the comment. If the comment is null, an empty comment is outputted.
      Specified by:
      writelnComment in interface CSVPrint
      Parameters:
      comment - the comment to output.
      Throws:
      IOException - if an error occurs while writing.
      Since:
      ostermillerutils 1.02.26
    • print

      public void print(String value)
      Print the string as the next value on the line. The value will be quoted if needed. If value is null, an empty value is printed.

      This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding println method.

      Specified by:
      print in interface CSVPrint
      Parameters:
      value - value to be outputted.
      Since:
      ostermillerutils 1.00.00
    • write

      public void write(String value) throws IOException
      Print the string as the next value on the line. The value will be quoted if needed. If value is null, an empty value is printed.
      Specified by:
      write in interface CSVPrint
      Parameters:
      value - value to be outputted.
      Throws:
      IOException - if an error occurs while writing.
      Since:
      ostermillerutils 1.02.26
    • flush

      public void flush() throws IOException
      Flush any data written out to underlying streams.
      Specified by:
      flush in interface CSVPrint
      Throws:
      IOException - if IO error occurs
      Since:
      ostermillerutils 1.02.26
    • close

      public void close() throws IOException
      Close any underlying streams.
      Specified by:
      close in interface CSVPrint
      Throws:
      IOException - if IO error occurs
      Since:
      ostermillerutils 1.02.26
    • checkError

      public boolean checkError()
      Flush the stream if it's not closed and check its error state. Errors are cumulative; once the stream encounters an error, this routine will return true on all successive calls.
      Specified by:
      checkError in interface CSVPrint
      Returns:
      True if the print stream has encountered an error, either on the underlying output stream or during a format conversion.
      Since:
      ostermillerutils 1.02.26
    • setAutoFlush

      public void setAutoFlush(boolean autoFlush)
      Set flushing behavior. Iff set, a flush command will be issued to any underlying stream after each print or write command.
      Specified by:
      setAutoFlush in interface CSVPrint
      Parameters:
      autoFlush - should auto flushing be enabled.
      Since:
      ostermillerutils 1.02.26
    • setAlwaysQuote

      public void setAlwaysQuote(boolean alwaysQuote)
      Set whether values printers should always be quoted, or whether the printer may, at its discretion, omit quotes around the value.
      Specified by:
      setAlwaysQuote in interface CSVPrint
      Parameters:
      alwaysQuote - true if quotes should be used even when not strictly needed.
      Since:
      ostermillerutils 1.02.26