java.lang.Object
io.jenetics.ext.util.CsvSupport
This class contains helper classes, which are the building blocks for handling
CSV files.
CsvSupport.LineReader
: This class allows you to read the lines of a CSV file. The result will be aStream
of CSV lines and are not split.CsvSupport.LineSplitter
: This class is responsible for splitting one CSV line into column values.CsvSupport.ColumnIndexes
: Allows to define the projection/embedding of the split/joined column values.CsvSupport.ColumnJoiner
: Joining a column array into a CSV line, which can be joined into a whole CSV string.
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
Holds the column indexes, which should be part of the split or join operation.static final class
This class joins an array of columns into one CSV line.static final class
This class reads CSV files and splits it into lines.static final class
Splitting a CSV line into columns (records).static final record
Holds the CSV column quote character.static final record
Holds the CSV column separator character. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Joins the given CSVcolumns
to one CSV line.static String
Joins the given CSVcolumns
to one CSV line.static String
Joins the given CSVcolumns
to one CSV line.Splits the CSV file, given by thereader
, into aStream
of CSV lines.parse
(CharSequence csv) Parses the given CSV string into a list of rows.static <T> List
<T> parse
(CharSequence csv, Function<? super String[], ? extends T> mapper) Parses the given CSV string into a list of records.static List
<double[]> parseDoubles
(CharSequence csv) Parses the given CSV string into a list ofdouble[]
array rows.readAllLines
(Reader reader) Splits the CSV file, given by thereader
, into aList
of CSV lines.readAllRows
(Reader reader) Splits the CSV file, given by thereader
, into aList
of CSV lines.Splits the CSV file, given by thereader
, into aStream
of CSV rows.static String[]
split
(CharSequence line) Splits a given CSVline
into columns.static Object[]
toComponents
(Record record) Converts the givenrecord
into its components.static Collector
<CharSequence, ?, String> toCsv()
Return a collector for joining a list of CSV rows into one CSV string.static Collector
<CharSequence, ?, String> Return a collector for joining a list of CSV rows into one CSV string.
-
Field Details
-
EOL
The newline string used for writing the CSV file:\r\n
.- See Also:
-
-
Method Details
-
lines
Splits the CSV file, given by thereader
, into aStream
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 givenreader
isnull
- See Also:
- API Note:
- The returned stream must be closed by the caller, which also closes the
CSV
reader
.
-
rows
Splits the CSV file, given by thereader
, into aStream
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 givenreader
isnull
- See Also:
- API Note:
- The returned stream must be closed by the caller, which also closes the
CSV
reader
.
-
readAllLines
Splits the CSV file, given by thereader
, into aList
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 givenreader
isnull
IOException
- if reading the CSV lines fails- See Also:
-
readAllRows
Splits the CSV file, given by thereader
, into aList
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 givenreader
isnull
IOException
- if reading the CSV lines fails- See Also:
-
parse
Parses the given CSV string into a list of records. The records are created from a row (String[]
array) by applying the givenmapper
.- Type Parameters:
T
- the record type- Parameters:
csv
- the CSV string to parsemapper
- the record mapper- Returns:
- the parsed record list
-
parse
Parses the given CSV string into a list of rows.- Parameters:
csv
- the CSV string to parse- Returns:
- the parsed CSV rows
-
parseDoubles
Parses the given CSV string into a list ofdouble[]
array rows.- Parameters:
csv
- the CSV string to parse- Returns:
- the parsed double data
-
split
Splits a given CSVline
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 givenline
isnull
-
join
Joins the given CSVcolumns
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 givencolumns
isnull
- See Also:
-
join
Joins the given CSVcolumns
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 givencolumns
isnull
- See Also:
-
join
Joins the given CSVcolumns
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 givencolumns
isnull
- See Also:
-
toComponents
Converts the givenrecord
into its components.- Parameters:
record
- the record to convert- Returns:
- the record components
-
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
Return a collector for joining a list of CSV rows into one CSV string. For the line breaks, the giveneol
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
-