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 voidcopy(TableReader reader, TableWriter writer)Copies content from one reader to another writer without header row.static voidcopy(TableReader reader, TableWriter writer, boolean copyHeaderRow)Copies content from one reader to another writer.static intcopy(java.lang.Object[][] arr, TableWriter writer)Copies the arrays from the iterator to the stream.static voidcopy(java.sql.ResultSet resultSet, TableWriter writer, boolean writeHeaderRow)Deprecated.static intcopy(java.util.Collection<? extends java.lang.Object[]> collection, TableWriter writer)Copies the arrays from the collection to the stream.static intcopy(java.util.Iterator<? extends java.lang.Object[]> i, TableWriter writer)Copies the arrays from the iterator to the stream.static voidcopy(javax.swing.JTable table, TableWriter writer, boolean writeHeaderRow, boolean selectedOnly)Deprecated.static <T> intcopyBeans(java.util.Collection<T> collection, TableWriter writer)Copies the beans from the collection to the stream.static <T> intcopyBeans(java.util.Iterator<T> i, TableWriter writer)Copies the beans from the collection to the stream.static <T> intcopyBeans(T[] arr, TableWriter writer)Copies the beans from the array to the stream.static voidcopyHeader(java.sql.ResultSet resultSet, TableWriter writer)Copies the header of the JDBC result set into the table writer.static voidcopyTableHeader(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.CharsetgetDefaultCharset()Returns thedefaultCharset.static voidsetDefaultCharset(java.lang.String defaultCharset)Sets thedefaultCharset.static voidsetDefaultCharset(java.nio.charset.Charset defaultCharset)Sets thedefaultCharset.
-
Constructor Details
-
CSVUtils
public CSVUtils()
-
-
Method Details
-
getDefaultCharset
public static java.nio.charset.Charset getDefaultCharset()Returns thedefaultCharset.- Returns:
- the defaultCharset
-
setDefaultCharset
public static void setDefaultCharset(java.nio.charset.Charset defaultCharset)Sets thedefaultCharset.- Parameters:
defaultCharset- the defaultCharset to set
-
setDefaultCharset
public static void setDefaultCharset(java.lang.String defaultCharset)Sets thedefaultCharset.- Parameters:
defaultCharset- the defaultCharset to set
-
copyHeader
public static void copyHeader(java.sql.ResultSet resultSet, TableWriter writer) throws java.lang.ExceptionCopies the header of the JDBC result set into the table writer.- Parameters:
resultSet- result set to copywriter- 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.ExceptionDeprecated.Copies the JDBC result set into the table writer.- Parameters:
resultSet- result set to copywriter- table writer to write towriteHeaderRow- 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.ExceptionCopies the table header into the table writer.- Parameters:
tableHeader- table header to get content fromwriter- 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.ExceptionDeprecated.Copies the table content into the table writer.- Parameters:
table- table to get content fromwriter- table writerwriteHeaderRow- whether header row shall be writtenselectedOnly- whether selected rows shall be written only- Throws:
java.lang.Exception- when an exception occurs
-
copy
Copies content from one reader to another writer without header row.- Parameters:
reader- reader to copy data fromwriter- 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.ExceptionCopies 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 fromwriter- writer to write data tocopyHeaderRow- 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.IOExceptionCopies the arrays from the collection to the stream.- Parameters:
collection- collection that contains rowswriter- 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.IOExceptionCopies the arrays from the iterator to the stream.- Parameters:
i- iterator that delivers rowswriter- writer to write rows to- Returns:
- number of rows written
- Throws:
java.io.IOException- when there is a problem with the writer.
-
copy
Copies the arrays from the iterator to the stream.- Parameters:
arr- two-dimensional array with rows and columnswriter- 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.IOExceptionCopies the beans from the collection to the stream. The method will useBeanWriter- Type Parameters:
T- The type of bean in the collection- Parameters:
collection- collection that contains JavaBeanswriter- 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.IOExceptionCopies the beans from the collection to the stream. The method will useBeanWriter- Type Parameters:
T- The type of bean in the collection- Parameters:
i- iterator that delivers JavaBeanswriter- writer to write rows to- Returns:
- number of rows written
- Throws:
java.io.IOException- when there is a problem with the writer.
-
copyBeans
Copies the beans from the array to the stream. The method will useBeanWriter- Type Parameters:
T- The type of bean in the array- Parameters:
arr- array of JavaBeanswriter- 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 returnminLength- 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 arrayminLength- 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 arrayminLength- 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 listminLength- minimum number of columns- Returns:
- array with at least minimum number of columns defined.
-