Package csv.util

Class CSVUtils

java.lang.Object
csv.util.CSVUtils

public class CSVUtils
extends java.lang.Object
Various methods for working with TableReader and TableWriter. This class provides useful method for easily copying table-like data from either JDBC results, JTable or table readers into table writers.
Author:
RalphSchuster
  • Constructor Summary

    Constructors 
    Constructor Description
    CSVUtils()  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.String[] convertArray​(java.lang.Object[] columns, int minLength)
    Returns an array from the columns.
    static java.lang.Object[] convertList​(java.util.List<?> columns, int minLength)
    Trims array to correct length of minimum column count.
    static void copy​(TableReader reader, TableWriter writer)
    Copies content from one reader to another writer without header row.
    static void copy​(TableReader reader, TableWriter writer, boolean copyHeaderRow)
    Copies content from one reader to another writer.
    static int copy​(java.lang.Object[][] arr, TableWriter writer)
    Copies the arrays from the iterator to the stream.
    static void copy​(java.sql.ResultSet resultSet, TableWriter writer, boolean writeHeaderRow)
    static int copy​(java.util.Collection<? extends java.lang.Object[]> collection, TableWriter writer)
    Copies the arrays from the collection to the stream.
    static int copy​(java.util.Iterator<? extends java.lang.Object[]> i, TableWriter writer)
    Copies the arrays from the iterator to the stream.
    static void copy​(javax.swing.JTable table, TableWriter writer, boolean writeHeaderRow, boolean selectedOnly)
    static <T> int copyBeans​(java.util.Collection<T> collection, TableWriter writer)
    Copies the beans from the collection to the stream.
    static <T> int copyBeans​(java.util.Iterator<T> i, TableWriter writer)
    Copies the beans from the collection to the stream.
    static <T> int copyBeans​(T[] arr, TableWriter writer)
    Copies the beans from the array to the stream.
    static void copyHeader​(java.sql.ResultSet resultSet, TableWriter writer)
    Copies the header of the JDBC result set into the table writer.
    static void copyTableHeader​(javax.swing.table.JTableHeader tableHeader, TableWriter writer)
    Copies the table header into the table writer.
    static java.lang.Object[] extendArray​(java.lang.Object[] columns, int minLength)
    Trims array to correct length of minimum column count.
    static java.lang.String[] extendArray​(java.lang.String[] columns, int minLength)
    Trims array to correct length of minimum column count.
    static java.nio.charset.Charset getDefaultCharset()
    Returns the defaultCharset.
    static void setDefaultCharset​(java.lang.String defaultCharset)
    Sets the defaultCharset.
    static void setDefaultCharset​(java.nio.charset.Charset defaultCharset)
    Sets the defaultCharset.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • getDefaultCharset

      public static java.nio.charset.Charset getDefaultCharset()
      Returns the defaultCharset.
      Returns:
      the defaultCharset
    • setDefaultCharset

      public static void setDefaultCharset​(java.nio.charset.Charset defaultCharset)
      Sets the defaultCharset.
      Parameters:
      defaultCharset - the defaultCharset to set
    • setDefaultCharset

      public static void setDefaultCharset​(java.lang.String defaultCharset)
      Sets the defaultCharset.
      Parameters:
      defaultCharset - the defaultCharset to set
    • copyHeader

      public static void copyHeader​(java.sql.ResultSet resultSet, TableWriter writer) throws java.lang.Exception
      Copies the header of the JDBC result set into the table writer.
      Parameters:
      resultSet - result set to copy
      writer - CSV writer to write to
      Throws:
      java.lang.Exception - if an exception occurs
    • copy

      @Deprecated public static void copy​(java.sql.ResultSet resultSet, TableWriter writer, boolean writeHeaderRow) throws java.lang.Exception
      Copies the JDBC result set into the table writer.
      Parameters:
      resultSet - result set to copy
      writer - table writer to write to
      writeHeaderRow - whether header row shall be written
      Throws:
      java.lang.Exception - if an exception occurs
    • copyTableHeader

      public static void copyTableHeader​(javax.swing.table.JTableHeader tableHeader, TableWriter writer) throws java.lang.Exception
      Copies the table header into the table writer.
      Parameters:
      tableHeader - table header to get content from
      writer - table writer
      Throws:
      java.lang.Exception - when an exception occurs
    • copy

      @Deprecated public static void copy​(javax.swing.JTable table, TableWriter writer, boolean writeHeaderRow, boolean selectedOnly) throws java.lang.Exception
      Copies the table content into the table writer.
      Parameters:
      table - table to get content from
      writer - table writer
      writeHeaderRow - whether header row shall be written
      selectedOnly - whether selected rows shall be written only
      Throws:
      java.lang.Exception - when an exception occurs
    • copy

      public static void copy​(TableReader reader, TableWriter writer) throws java.lang.Exception
      Copies content from one reader to another writer without header row.
      Parameters:
      reader - reader to copy data from
      writer - writer to write data to
      Throws:
      java.lang.Exception - when an exception occurs
    • copy

      public static void copy​(TableReader reader, TableWriter writer, boolean copyHeaderRow) throws java.lang.Exception
      Copies content from one reader to another writer. The header row will only be written if the reader delivers such a row.
      Parameters:
      reader - reader to copy data from
      writer - writer to write data to
      copyHeaderRow - whether a header row shall be copied too (only if reader supports it)
      Throws:
      java.lang.Exception - when an exception occurs
    • copy

      public static int copy​(java.util.Collection<? extends java.lang.Object[]> collection, TableWriter writer) throws java.io.IOException
      Copies the arrays from the collection to the stream.
      Parameters:
      collection - collection that contains rows
      writer - writer to write rows to
      Returns:
      number of rows written
      Throws:
      java.io.IOException - when there is a problem with the writer.
    • copy

      public static int copy​(java.util.Iterator<? extends java.lang.Object[]> i, TableWriter writer) throws java.io.IOException
      Copies the arrays from the iterator to the stream.
      Parameters:
      i - iterator that delivers rows
      writer - writer to write rows to
      Returns:
      number of rows written
      Throws:
      java.io.IOException - when there is a problem with the writer.
    • copy

      public static int copy​(java.lang.Object[][] arr, TableWriter writer) throws java.io.IOException
      Copies the arrays from the iterator to the stream.
      Parameters:
      arr - two-dimensional array with rows and columns
      writer - writer to write rows to
      Returns:
      number of rows written
      Throws:
      java.io.IOException - when there is a problem with the writer.
    • copyBeans

      public static <T> int copyBeans​(java.util.Collection<T> collection, TableWriter writer) throws java.io.IOException
      Copies the beans from the collection to the stream. The method will use BeanWriter
      Type Parameters:
      T - The type of bean in the collection
      Parameters:
      collection - collection that contains JavaBeans
      writer - writer to write rows to
      Returns:
      number of rows written
      Throws:
      java.io.IOException - when there is a problem with the writer.
    • copyBeans

      public static <T> int copyBeans​(java.util.Iterator<T> i, TableWriter writer) throws java.io.IOException
      Copies the beans from the collection to the stream. The method will use BeanWriter
      Type Parameters:
      T - The type of bean in the collection
      Parameters:
      i - iterator that delivers JavaBeans
      writer - writer to write rows to
      Returns:
      number of rows written
      Throws:
      java.io.IOException - when there is a problem with the writer.
    • copyBeans

      public static <T> int copyBeans​(T[] arr, TableWriter writer) throws java.io.IOException
      Copies the beans from the array to the stream. The method will use BeanWriter
      Type Parameters:
      T - The type of bean in the array
      Parameters:
      arr - array of JavaBeans
      writer - writer to write rows to
      Returns:
      number of rows written
      Throws:
      java.io.IOException - when there is a problem with the writer.
    • convertArray

      public static java.lang.String[] convertArray​(java.lang.Object[] columns, int minLength)
      Returns an array from the columns. This function exists for convinience to take care of minimum column count.
      Parameters:
      columns - columns to return
      minLength - minimum number of columns in return array
      Returns:
      arrray with column values
    • extendArray

      public static java.lang.Object[] extendArray​(java.lang.Object[] columns, int minLength)
      Trims array to correct length of minimum column count.
      Parameters:
      columns - columns array
      minLength - minimum number of columns
      Returns:
      array with at least minimum number of columns defined.
    • extendArray

      public static java.lang.String[] extendArray​(java.lang.String[] columns, int minLength)
      Trims array to correct length of minimum column count.
      Parameters:
      columns - columns array
      minLength - minimum number of columns
      Returns:
      array with at least minimum number of columns defined.
    • convertList

      public static java.lang.Object[] convertList​(java.util.List<?> columns, int minLength)
      Trims array to correct length of minimum column count.
      Parameters:
      columns - columns list
      minLength - minimum number of columns
      Returns:
      array with at least minimum number of columns defined.