Skip to content

Commit ec80f0e

Browse files
committed
add ExampleCsvReaderWithSpecialQuotedFieldHandling
1 parent 208677d commit ec80f0e

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

docs/src/content/docs/guides/Examples/quote-strategies.mdx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ You may also check the corresponding
2929
[Javadoc](https://javadoc.io/doc/de.siegmar/fastcsv/latest/de.siegmar.fastcsv/de/siegmar/fastcsv/writer/QuoteStrategies.html)
3030
for more information.
3131

32-
## Example
32+
## Writing fields with different quote strategies
3333

3434
In the following example, CSV output is created using different quote strategies.
3535

3636
<SourceExample filename="ExampleCsvWriterWithQuoteStrategy.java" highlights={[/\.quoteStrategy\(.*\)/g]}/>
3737

38-
:::tip
39-
If you want to know if a field is enclosed in quotes when reading a CSV file, you may implement a
40-
custom callback handler. Check out the [Custom Callback handler example](/guides/examples/custom-callback-handler/)
41-
and especially the
42-
[addField() method](https://javadoc.io/doc/de.siegmar/fastcsv/latest/de.siegmar.fastcsv/de/siegmar/fastcsv/reader/CsvCallbackHandler.html).
43-
:::
38+
## Reading quoted fields
39+
40+
To determine if a field is enclosed in quotes when reading a CSV file,
41+
additional work and an extra object per field are required.
42+
FastCSV does not support this feature out of the box, but it can be implemented using a custom callback handler.
43+
44+
<SourceExample filename="ExampleCsvReaderWithSpecialQuotedFieldHandling.java" highlights={['QuotableFieldHandler', 'QuotableField']}/>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package example;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import de.siegmar.fastcsv.reader.AbstractBaseCsvCallbackHandler;
7+
import de.siegmar.fastcsv.reader.CsvReader;
8+
import de.siegmar.fastcsv.reader.RecordWrapper;
9+
10+
/**
11+
* Example for reading CSV data with custom handling of quoted fields.
12+
*/
13+
public class ExampleCsvReaderWithSpecialQuotedFieldHandling {
14+
15+
private static final String DATA = """
16+
"quoted foo",unquoted foo
17+
""";
18+
19+
public static void main(String[] args) {
20+
CsvReader.builder()
21+
.skipEmptyLines(false)
22+
.build(new QuotableFieldHandler(), DATA)
23+
.forEach(System.out::println);
24+
}
25+
26+
static class QuotableFieldHandler extends AbstractBaseCsvCallbackHandler<List<QuotableField>> {
27+
28+
private final List<QuotableField> fields = new ArrayList<>();
29+
30+
@Override
31+
protected void handleField(final int fieldIdx, final char[] buf, final int offset, final int len, final boolean quoted) {
32+
final String value = new String(buf, offset, len);
33+
fields.add(new QuotableField(value, quoted));
34+
}
35+
36+
@Override
37+
protected void handleBegin(final long startingLineNumber) {
38+
fields.clear();
39+
}
40+
41+
@Override
42+
protected RecordWrapper<List<QuotableField>> buildRecord() {
43+
return wrapRecord(List.copyOf(fields));
44+
}
45+
46+
}
47+
48+
record QuotableField(String field, boolean quoted) {
49+
}
50+
51+
}

0 commit comments

Comments
 (0)