The Apache Commons CSV library can deal with comments by attaching the comment to the following record. Is there a way to access comments that come before the header line?
In the code below is there a way to access the text "leading" "comments" which comes at the start of the file?
I can do this by not specifying setHeaders in the format but then I don't get the header processing.
I know I can't get the "trailing comment", I understand why and I'm ok with that.
package com.nowhere;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
try (Reader reader = new StringReader(DATA); CSVParser parser = CSVParser.parse(reader, FORMAT)) {
for (CSVRecord rec : parser) {
System.out.println(rec);
}
}
}
public static final CSVFormat FORMAT = CSVFormat.Builder.create(CSVFormat.EXCEL).setCommentMarker(';').setHeader().build();
public static final String DATA = """
; leading
; comments
"a","b","c"
1,2,3
; trailing comment
""";
}