Package csv.util

Class CSVUtils

java.lang.Object
csv.util.CSVUtils

public class CSVUtils extends 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 Details

    • CSVUtils

      public CSVUtils()
  • Method Details

    • getDefaultCharset

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

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

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

      public static void copyHeader(ResultSet resultSet, TableWriter writer) throws 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:
      Exception - if an exception occurs
    • copyTableHeader

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

      public static void copy(TableReader reader, TableWriter writer) throws 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:
      Exception - when an exception occurs
    • copy

      public static void copy(TableReader reader, TableWriter writer, boolean copyHeaderRow) throws 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:
      Exception - when an exception occurs
    • copy

      public static int copy(Collection<? extends Object[]> collection, TableWriter writer) throws 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:
      IOException - when there is a problem with the writer.
    • copy

      public static int copy(Iterator<? extends Object[]> i, TableWriter writer) throws 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:
      IOException - when there is a problem with the writer.
    • copy

      public static int copy(Object[][] arr, TableWriter writer) throws 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:
      IOException - when there is a problem with the writer.
    • copyBeans

      public static <T> int copyBeans(Collection<T> collection, TableWriter writer) throws 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:
      IOException - when there is a problem with the writer.
    • copyBeans

      public static <T> int copyBeans(Iterator<T> i, TableWriter writer) throws 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:
      IOException - when there is a problem with the writer.
    • copyBeans

      public static <T> int copyBeans(T[] arr, TableWriter writer) throws 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:
      IOException - when there is a problem with the writer.
    • convertArray

      public static String[] convertArray(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 Object[] extendArray(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 String[] extendArray(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 Object[] convertList(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.