Class Csv

java.lang.Object
com.google.common.labs.csv.Csv

public final class Csv extends Object
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 Details

  • Method Details

    • withDelimiter

      public Csv withDelimiter(char delimiter)
      Returns an otherwise equivalent CSV parser but using delimiter instead of comma.
    • withComments

      public Csv 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

      public <A,R> Stream<R> parse(String csv, Collector<? super String, A, R> rowCollector)
      Parses csv string lazily, returning one row at a time in a stream, with field values collected by rowCollector.

      No special treatment of the header row. If you know you have a header row, consider calling .skip(1) to skip it, or use parseToMaps(java.lang.String) with the field names as the Map keys.

    • parseToMaps

      public Stream<Map<String,String>> parseToMaps(String csv)
      Parses csv string lazily, returning each row in a Map keyed by the field names in the header row. The first non-empty row is expected to be the header row.
    • toString

      public String toString()
      Overrides:
      toString in class Object