Class Csv
java.lang.Object
com.google.common.labs.csv.Csv
An easy-to-use CSV parser with lazy parsing support.
For example:
import static com.google.common.labs.csv.Csv.CSV;
List<List<String>> rows =
// skip(1) to skip the header row.
CSV.parse(input, toUnmodifiableList()).skip(1).toList();
Or, if the order and the number of fields are known at compile-time, you could directly combine them to build objects of your choice:
import com.google.mu.util.stream.MoreCollectors.combining;
import static com.google.common.labs.csv.Csv.CSV;
List<Result> results =
// assuming no header row
CSV.parse(input, combining((foo, bar, baz) -> new Result(foo, bar, baz))).toList();
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescription<A,
R> Stream <R> Parsescsv
string lazily, returning one row at a time in a stream, with field values collected byrowCollector
.parseToMaps
(String csv) Parsescsv
string lazily, returning each row in aMap
keyed by the field names in the header row.toString()
Returns an otherwise equivalent CSV parser but allows comment rows.withDelimiter
(char delimiter) Returns an otherwise equivalent CSV parser but usingdelimiter
instead of comma.
-
Field Details
-
CSV
Default CSV parser. Configurable usingwithComments()
andwithDelimiter(char)
.
-
-
Method Details
-
withDelimiter
Returns an otherwise equivalent CSV parser but usingdelimiter
instead of comma. -
withComments
Returns an otherwise equivalent CSV parser but allows comment rows.Comments are recognized by the presence of a hash sign (#) at the beginning of a line.
Note that comments are not standard CSV specification.
-
parse
Parsescsv
string lazily, returning one row at a time in a stream, with field values collected byrowCollector
.No special treatment of the header row. If you know you have a header row, consider calling
.skip(1)
to skip it, or useparseToMaps(java.lang.String)
with the field names as the Map keys. -
parseToMaps
-
toString
-