Class CsvSupport

java.lang.Object
io.jenetics.ext.util.CsvSupport

public final class CsvSupport extends Object
This class contains helper classes, which are the building blocks for handling CSV files.

Additionally, this class contains a set of helper methods for CSV handling using default configurations.

Reading and splitting CSV lines

final List<String[]> rows;
try (Stream<String> lines = CsvSupport.lines(new FileReader("data.csv"))) {
	rows = lines
		.map(CsvSupport::split)
		.toList();
}

Joining columns and creating CSV string

final String[][] data = ...;
final String csv = Arrays.stream(data)
	.map(CsvSupport::join)
	.collect(CsvSupport.toCsv());

Parsing CSV string

final List<String[]> rows = CsvSupport.parse("""
	# Country,City,AccentCity,Region,Population,Latitude,Longitude
	ad,aixas,AixĂ s,06,,42.4833333,1.4666667
	ad,aixirivali,Aixirivali,06,,42.4666667,1.5
	"""
);

Parsing double values, given as CSV string

Another example is to parse double values, which are given as CSV string and use this data for running a regression analysis.

final List<double[]> values = CsvSupport.parseDoubles("""
	0.0,0.0000
	0.1,0.0740
	0.2,0.1120
	0.3,0.1380
	0.4,0.1760
	0.5,0.2500
	0.6,0.3840
	0.7,0.6020
	0.8,0.9280
	0.9,1.3860
	1.0,2.0000
	"""
);
Since:
8.1
Version:
8.1
See Also:
  • Field Details

  • Method Details

    • lines

      public static Stream<String> lines(Reader reader)
      Splits the CSV file, given by the reader, into a Stream of CSV lines. The CSV is split at line breaks, as long as they are not part of a quoted column. For reading the CSV lines, the default quote character, CsvSupport.Quote.DEFAULT, is used.
      Parameters:
      reader - the CSV source reader. The reader is automatically closed when the returned line stream is closed.
      Returns:
      the stream of CSV lines
      Throws:
      NullPointerException - if the given reader is null
      See Also:
      API Note:
      The returned stream must be closed by the caller, which also closes the CSV reader.
    • rows

      public static Stream<String[]> rows(Reader reader)
      Splits the CSV file, given by the reader, into a Stream of CSV rows. The CSV is split at line breaks, as long as they are not part of a quoted column. For reading the CSV lines, the default quote character, CsvSupport.Quote.DEFAULT, is used. Then each line is split into its columns using the default separator character.
      Parameters:
      reader - the CSV source reader. The reader is automatically closed when the returned line stream is closed.
      Returns:
      the stream of CSV rows
      Throws:
      NullPointerException - if the given reader is null
      See Also:
      API Note:
      The returned stream must be closed by the caller, which also closes the CSV reader.
    • readAllLines

      public static List<String> readAllLines(Reader reader) throws IOException
      Splits the CSV file, given by the reader, into a List of CSV lines. The CSV is split at line breaks, as long as they are not part of a quoted column. For reading the CSV lines, the default quote character, CsvSupport.Quote.DEFAULT, is used.
      Parameters:
      reader - the reader stream to split into CSV lines
      Returns:
      the list of CSV lines
      Throws:
      NullPointerException - if the given reader is null
      IOException - if reading the CSV lines fails
      See Also:
    • readAllRows

      public static List<String[]> readAllRows(Reader reader) throws IOException
      Splits the CSV file, given by the reader, into a List of CSV lines. The CSV is split at line breaks, as long as they are not part of a quoted column. For reading the CSV lines, the default quote character, CsvSupport.Quote.DEFAULT, is used. Then each line is split into its columns using the default separator character.
      Parameters:
      reader - the reader stream to split into CSV lines
      Returns:
      the list of CSV rows
      Throws:
      NullPointerException - if the given reader is null
      IOException - if reading the CSV lines fails
      See Also:
    • parse

      public static <T> List<T> parse(CharSequence csv, Function<? super String[],? extends T> mapper)
      Parses the given CSV string into a list of records. The records are created from a row (String[] array) by applying the given mapper.
      Type Parameters:
      T - the record type
      Parameters:
      csv - the CSV string to parse
      mapper - the record mapper
      Returns:
      the parsed record list
    • parse

      public static List<String[]> parse(CharSequence csv)
      Parses the given CSV string into a list of rows.
      Parameters:
      csv - the CSV string to parse
      Returns:
      the parsed CSV rows
    • parseDoubles

      public static List<double[]> parseDoubles(CharSequence csv)
      Parses the given CSV string into a list of double[] array rows.
      Parameters:
      csv - the CSV string to parse
      Returns:
      the parsed double data
    • split

      public static String[] split(CharSequence line)
      Splits a given CSV line into columns. The default values for the separator and quote character are used (CsvSupport.Separator.DEFAULT, CsvSupport.Quote.DEFAULT) for splitting the line.
      Parameters:
      line - the CSV line to split
      Returns:
      the split CSV lines
      Throws:
      NullPointerException - if the given line is null
    • join

      public static String join(Iterable<?> columns)
      Joins the given CSV columns to one CSV line. The default values for the separator and quote character are used (CsvSupport.Separator.DEFAULT, CsvSupport.Quote.DEFAULT) for joining the columns.
      Parameters:
      columns - the CSV columns to join
      Returns:
      the CSV line, joined from the given columns
      Throws:
      NullPointerException - if the given columns is null
      See Also:
    • join

      public static String join(Object[] columns)
      Joins the given CSV columns to one CSV line. The default values for the separator and quote character are used (CsvSupport.Separator.DEFAULT, CsvSupport.Quote.DEFAULT) for joining the columns.
      Parameters:
      columns - the CSV columns to join
      Returns:
      the CSV line, joined from the given columns
      Throws:
      NullPointerException - if the given columns is null
      See Also:
    • join

      public static String join(String... columns)
      Joins the given CSV columns to one CSV line. The default values for the separator and quote character are used (CsvSupport.Separator.DEFAULT, CsvSupport.Quote.DEFAULT) for joining the columns.
      Parameters:
      columns - the CSV columns to join
      Returns:
      the CSV line, joined from the given columns
      Throws:
      NullPointerException - if the given columns is null
      See Also:
    • toComponents

      public static Object[] toComponents(Record record)
      Converts the given record into its components.
      Parameters:
      record - the record to convert
      Returns:
      the record components
    • toCsv

      public static Collector<CharSequence,?,String> toCsv()
      Return a collector for joining a list of CSV rows into one CSV string.
      Returns:
      a collector for joining a list of CSV rows into one CSV string
    • toCsv

      public static Collector<CharSequence,?,String> toCsv(String eol)
      Return a collector for joining a list of CSV rows into one CSV string. For the line breaks, the given eol sequence is used.
      Parameters:
      eol - the end of line sequence used for line breaks
      Returns:
      a collector for joining a list of CSV rows into one CSV string