Package com.gengoai.io
Class CSV
- java.lang.Object
-
- com.gengoai.io.CSV
-
- All Implemented Interfaces:
Serializable
public class CSV extends Object implements Serializable
Specification of a delimited separated file, or more commonly refereed to as CSV. Provides methods to build a CSV specification and then create a reader, writer, or formatter.
- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CSV()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static CSV
builder()
Creates a CSV builder objectCSV
comment(char commentChar)
Sets the character that signifies a commentstatic CSV
csv()
Convenience method for quickly retrieving a csv version of a CSV builderCSV
delimiter(char delimiter)
Sets the delimiter characterCSV
escape(char escape)
Sets the escape characterCSVFormatter
formatter()
Creates a CSVFormatter using this specificationchar
getComment()
Gets the comment character.char
getDelimiter()
Gets the delimiter character.char
getEscape()
Gets the escape character.boolean
getHasHeader()
Determines if the CSV file is expected to have a header or notList<String>
getHeader()
Gets the name of the column headers.char
getQuote()
Gets the quote character.CSV
hasHeader()
Specifies that the CSV file has a header.CSV
hasHeader(boolean hasHeader)
Specifies whether or not the CSV file has a header.CSV
header(String... items)
Specifies the names of the items in the CSV header.CSV
header(List<String> items)
Specifies the names of the items in the CSV header.boolean
isKeepEmptyCells()
Determines if empty cells should be kept or notIterator<List<String>>
iterator(@NonNull Resource resource)
Creates an iterator over the rows of the csv fileCSV
keepEmptyCells()
Specifies that empty cells should be keptCSV
quote(char quote)
Sets the quote characterCSVReader
reader(Resource resource)
Creates a CSVReader using this specification from a given resourceCSVReader
reader(Reader reader)
Creates a CSVReader using this specification from a given readerCSV
removeEmptyCells()
Specifies that empty cells should be removedStream<List<String>>
rowListStream(@NonNull Resource resource)
Creates a reusable Java Stream over the rows of the csv fileStream<Map<String,String>>
rowMapStream(@NonNull Resource resource)
Creates a reusable Java Stream over the rows of the csv file returning them as map with key as the column name and value as the value.static CSV
tsv()
Convenience method for quickly retrieving a tsv version of a CSV builderCSVWriter
writer(Resource resource)
Creates a CSVWriter using this specification from a given resourceCSVWriter
writer(Writer writer)
Creates a CSVWriter using this specification from a given writer
-
-
-
Method Detail
-
builder
public static CSV builder()
Creates a CSV builder object
- Returns:
- the csv builder builder object
-
csv
public static CSV csv()
Convenience method for quickly retrieving a csv version of a CSV builder
- Returns:
- the csv builder builder with delimiter set to a comma character
-
tsv
public static CSV tsv()
Convenience method for quickly retrieving a tsv version of a CSV builder
- Returns:
- the csv builder builder with delimiter set to a tab character
-
comment
public CSV comment(char commentChar)
Sets the character that signifies a comment- Parameters:
commentChar
- the comment char- Returns:
- the csv builder
-
delimiter
public CSV delimiter(char delimiter)
Sets the delimiter character- Parameters:
delimiter
- the delimiter- Returns:
- the csv builder
-
escape
public CSV escape(char escape)
Sets the escape character- Parameters:
escape
- the escape- Returns:
- the csv builder
-
formatter
public CSVFormatter formatter()
Creates a CSVFormatter using this specification- Returns:
- the CSVFormatter
-
getComment
public char getComment()
Gets the comment character.- Returns:
- the comment character.
-
getDelimiter
public char getDelimiter()
Gets the delimiter character.- Returns:
- the delimiter character
-
getEscape
public char getEscape()
Gets the escape character.- Returns:
- the escape character
-
getHasHeader
public boolean getHasHeader()
Determines if the CSV file is expected to have a header or not- Returns:
- True if the first line of the csv file is the header, false if there is no header
-
getHeader
public List<String> getHeader()
Gets the name of the column headers.- Returns:
- the names of the column headers
-
getQuote
public char getQuote()
Gets the quote character.- Returns:
- the quote character
-
hasHeader
public CSV hasHeader()
Specifies that the CSV file has a header.- Returns:
- the csv builder builder
-
hasHeader
public CSV hasHeader(boolean hasHeader)
Specifies whether or not the CSV file has a header.- Parameters:
hasHeader
- true the csv has a header, false it does not- Returns:
- the csv builder
-
header
public CSV header(String... items)
Specifies the names of the items in the CSV header.- Parameters:
items
- the names of the columns (i.e. header names)- Returns:
- the csv builder
-
header
public CSV header(List<String> items)
Specifies the names of the items in the CSV header.- Parameters:
items
- the names of the columns (i.e. header names)- Returns:
- the csv builder
-
isKeepEmptyCells
public boolean isKeepEmptyCells()
Determines if empty cells should be kept or not- Returns:
- True if empty cells are kept, False if they are removed
-
iterator
public Iterator<List<String>> iterator(@NonNull @NonNull Resource resource) throws IOException
Creates an iterator over the rows of the csv file- Parameters:
resource
- the resource to read from- Returns:
- the stream of items in the csv file
- Throws:
IOException
- Something went wrong reading the file
-
keepEmptyCells
public CSV keepEmptyCells()
Specifies that empty cells should be kept- Returns:
- the csv builder
-
quote
public CSV quote(char quote)
Sets the quote character- Parameters:
quote
- the quote- Returns:
- the csv builder
-
reader
public CSVReader reader(Reader reader) throws IOException
Creates a CSVReader using this specification from a given reader- Parameters:
reader
- the reader to wrap- Returns:
- The CSVReader
- Throws:
IOException
- Something went wrong initializing the reader
-
reader
public CSVReader reader(Resource resource) throws IOException
Creates a CSVReader using this specification from a given resource- Parameters:
resource
- the resource to read from- Returns:
- The CSVReader
- Throws:
IOException
- Something went wrong initializing the resource
-
removeEmptyCells
public CSV removeEmptyCells()
Specifies that empty cells should be removed- Returns:
- the csv builder
-
rowListStream
public Stream<List<String>> rowListStream(@NonNull @NonNull Resource resource) throws IOException
Creates a reusable Java Stream over the rows of the csv file- Parameters:
resource
- the resource to read from- Returns:
- the stream of items in the csv file
- Throws:
IOException
- Something went wrong reading the file
-
rowMapStream
public Stream<Map<String,String>> rowMapStream(@NonNull @NonNull Resource resource) throws IOException
Creates a reusable Java Stream over the rows of the csv file returning them as map with key as the column name and value as the value. When the column is not in the header the column name will be "AutoColumn-" and column number.- Parameters:
resource
- the resource to read from- Returns:
- the stream of items in the csv file
- Throws:
IOException
- Something went wrong reading the file
-
writer
public CSVWriter writer(Writer writer) throws IOException
Creates a CSVWriter using this specification from a given writer- Parameters:
writer
- the writer to wrap- Returns:
- The CSVWriter
- Throws:
IOException
- Something went wrong initializing the writer
-
writer
public CSVWriter writer(Resource resource) throws IOException
Creates a CSVWriter using this specification from a given resource- Parameters:
resource
- the resource to write to- Returns:
- The CSVWriter
- Throws:
IOException
- Something went wrong initializing the resource
-
-