java.lang.Object
io.jenetics.ext.util.CsvSupport.LineSplitter
- Enclosing class:
CsvSupport
Splitting a CSV line into columns (records).
Projecting and re-ordering columns
Examples
Simple usagefinal var reader = new LineReader();
final var splitter = new LineSplitter(new Separator(','));
final List<String[]> rows;
try (Stream<String> lines = reader.read(CharBuffer.wrap(csv))) {
rows = lines
.map(splitter::split)
.toList();
}
final var csv = """
Country,City,AccentCity,Region,Population,Latitude,Longitude
ad,aixas,AixĂ s,06,,42.4833333,1.4666667
ad,aixirivali,Aixirivali,06,,42.4666667,1.5
ad,aixirivall,Aixirivall,06,,42.4666667,1.5
ad,aixirvall,Aixirvall,06,,42.4666667,1.5
ad,aixovall,Aixovall,06,,42.4666667,1.4833333
""";
final var reader = new LineReader();
// Only read three columns, in the specified order.
final var projection = new ColumnIndexes(
// Read 'Region' as first column.
3,
// Read 'City' as second column.
1,
// Read 'Country' as third column.
0
);
// Configure the splitter with default separator and quote character,
// and make it return only the specified columns in the defined order.
final var splitter = new LineSplitter(projection);
try (Stream<String> lines = reader.read(CharBuffer.wrap(csv))) {
final var result = lines
.map(splitter::split)
.map(Arrays::toString)
.collect(Collectors.joining("\n", "", "\n"));
assert result.equals("""
[Region, City, Country]
[06, aixas, ad]
[06, aixirivali, ad]
[06, aixirivall, ad]
[06, aixirvall, ad]
[06, aixovall, ad]
"""
);
}
- Since:
- 8.1
- Version:
- 8.1
- API Note:
- A line splitter ist not thread-safe and can't be shared between different threads.
- Implementation Note:
- The split
String[]array will never containnullvalues. Empty columns will be returned as empty strings.
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new line splitter with default values.LineSplitter(CsvSupport.ColumnIndexes projection) Create a new line splitter with the given parameters.LineSplitter(CsvSupport.Quote quote) Create a new line splitter with the given parameters.LineSplitter(CsvSupport.Separator separator) Create a new line splitter with the given parameters.LineSplitter(CsvSupport.Separator separator, CsvSupport.Quote quote) Create a new line splitter with the given parameters.LineSplitter(CsvSupport.Separator separator, CsvSupport.Quote quote, CsvSupport.ColumnIndexes projection) Create a new line splitter with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionString[]split(CharSequence line) Splitting the given CSVlineinto its columns.
-
Constructor Details
-
LineSplitter
public LineSplitter(CsvSupport.Separator separator, CsvSupport.Quote quote, CsvSupport.ColumnIndexes projection) Create a new line splitter with the given parameters.- Parameters:
separator- the separator character used by the CSV line to splitquote- the quote character used by the CSV line to splitprojection- the column indexes which should be part of the split result- Throws:
NullPointerException- if one of the parameters isnull
-
LineSplitter
Create a new line splitter with the given parameters.- Parameters:
separator- the separator character used by the CSV line to splitquote- the quote character used by the CSV line to split- Throws:
NullPointerException- if one of the parameters isnull
-
LineSplitter
Create a new line splitter with the given parameters. The default quote character,CsvSupport.Quote.DEFAULT, will be used by the created splitter.- Parameters:
separator- the separator character used by the CSV line to split- Throws:
NullPointerException- if one of the parameters isnull
-
LineSplitter
Create a new line splitter with the given parameters. The default separator character,CsvSupport.Separator.DEFAULT, will be used by the created splitter.- Parameters:
quote- the quote character used by the CSV line to split- Throws:
NullPointerException- if one of the parameters isnull
-
LineSplitter
Create a new line splitter with the given parameters. Only the defined columns will be part of the split result and the default separator character,CsvSupport.Separator.DEFAULT, and default quote character,CsvSupport.Quote.DEFAULT, is used by the created splitter.- Parameters:
projection- the column indexes which should be part of the split result- Throws:
NullPointerException- if one of the parameters isnull
-
LineSplitter
public LineSplitter()Create a new line splitter with default values.
-
-
Method Details
-
split
Splitting the given CSVlineinto its columns.- Parameters:
line- the CSV line to split- Returns:
- the split CSV columns
- Throws:
NullPointerException- if the CSVlineisnull- Implementation Note:
- The split
String[]array will never containnullvalues. Empty columns will be returned as empty strings.
-