@Incubating
public class CsvSlurper
extends Object
Represents a CSV parser.
Usage:
def csv = new groovy.csv.CsvSlurper().parseText('name,age\nAlice,30\nBob,25')
assert csv[0].name == 'Alice'
assert csv[1].age == '25'
| Constructor and description |
|---|
CsvSlurper()Creates a slurper that uses comma-separated columns, double quotes, and treats the first row as headers. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public List<Map<String, String>> |
parse(Reader reader)Parse CSV from a reader. |
|
public List<Map<String, String>> |
parse(InputStream stream)Parse CSV from a UTF-8 encoded input stream. |
|
public List<Map<String, String>> |
parse(InputStream stream, Charset charset)Parse CSV from an input stream decoded with the given charset. |
|
public List<Map<String, String>> |
parse(File file)Parse CSV from a UTF-8 encoded file. |
|
public List<Map<String, String>> |
parse(File file, Charset charset)Parse CSV from a file decoded with the given charset. |
|
public List<Map<String, String>> |
parse(Path path)Parse CSV from a UTF-8 encoded path. |
|
public List<Map<String, String>> |
parse(Path path, Charset charset)Parse CSV from a path decoded with the given charset. |
<T> |
public List<T> |
parseAs(Class<T> type, String csv)Parse CSV into typed objects using Jackson databinding. |
<T> |
public List<T> |
parseAs(Class<T> type, Reader reader)Parse CSV from a reader into typed objects. |
<T> |
public List<T> |
parseAs(Class<T> type, File file)Parse CSV from a UTF-8 encoded file into typed objects. |
<T> |
public List<T> |
parseAs(Class<T> type, File file, Charset charset)Parse CSV from a file decoded with the given charset into typed objects. |
<T> |
public List<T> |
parseAs(Class<T> type, Path path)Parse CSV from a UTF-8 encoded path into typed objects. |
<T> |
public List<T> |
parseAs(Class<T> type, Path path, Charset charset)Parse CSV from a path decoded with the given charset into typed objects. |
|
public List<Map<String, String>> |
parseText(String csv)Parse the content of the specified CSV text. |
|
public CsvSlurper |
setColumns(String columns)Set explicit column names, typically for CSV without a header row. |
|
public CsvSlurper |
setColumns(List<String> columns)Set explicit column names, typically for CSV without a header row. |
|
public CsvSlurper |
setQuoteChar(char quoteChar)Set the quote character (default: double-quote). |
|
public CsvSlurper |
setSeparator(char separator)Set the column separator character (default: comma). |
|
public CsvSlurper |
setUseHeader(boolean useHeader)Set whether the first row is a header row (default: true). |
Creates a slurper that uses comma-separated columns, double quotes, and treats the first row as headers.
Parse CSV from a reader.
When explicit column names have been supplied via setColumns(String...),
each row is returned as a map keyed by those names. Otherwise, when
useHeader is true (the default), maps are keyed by column headers from
the first row. Otherwise, maps are keyed by generated column names
c1, c2, ...
reader - the reader of CSVParse CSV from a UTF-8 encoded input stream. The caller is responsible for closing the stream.
stream - the input stream of CSVParse CSV from an input stream decoded with the given charset. The caller is responsible for closing the stream.
stream - the input stream of CSVcharset - the charset used to decode the streamParse CSV from a UTF-8 encoded file.
file - the CSV fileParse CSV from a file decoded with the given charset.
file - the CSV filecharset - the charset used to decode the fileParse CSV from a UTF-8 encoded path.
path - the path to the CSV fileParse CSV from a path decoded with the given charset.
path - the path to the CSV filecharset - the charset used to decode the file Parse CSV into typed objects using Jackson databinding.
Supports @JsonProperty and @JsonFormat annotations for
column mapping and type conversion.
type - the target typecsv - the CSV textT - the target type Parse CSV from a reader into typed objects.
When explicit column names have been supplied via setColumns(String...),
values are bound to properties of those names by position. Otherwise, when
useHeader is true (the default), values are bound by the column names
in the header row. Otherwise, positional columns are derived from the target
type's properties: component declaration order for records, alphabetical
order for other classes. Use @JsonPropertyOrder on the type to set
an explicit column order.
type - the target typereader - the reader of CSVT - the target typeParse CSV from a UTF-8 encoded file into typed objects.
type - the target typefile - the CSV fileT - the target typeParse CSV from a file decoded with the given charset into typed objects.
type - the target typefile - the CSV filecharset - the charset used to decode the fileT - the target typeParse CSV from a UTF-8 encoded path into typed objects.
type - the target typepath - the path to the CSV fileT - the target typeParse CSV from a path decoded with the given charset into typed objects.
type - the target typepath - the path to the CSV filecharset - the charset used to decode the fileT - the target typeParse the content of the specified CSV text.
csv - the CSV text Set explicit column names, typically for CSV without a header row.
When column names are supplied and useHeader is true, the first
row is still consumed as a header row but the supplied names take
precedence over the names it contains.
columns - the column names Set explicit column names, typically for CSV without a header row.
When column names are supplied and useHeader is true, the first
row is still consumed as a header row but the supplied names take
precedence over the names it contains.
columns - the column namesSet the quote character (default: double-quote).
quoteChar - the quote characterSet the column separator character (default: comma).
separator - the separator characterSet whether the first row is a header row (default: true).
useHeader - true to treat the first row as headersCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.