I'm using CSVParser and CSVReader for parsing and reading csv-files.
char QUOTE = '""';
InputStreamReader reader = new InputStreamReader(content);
CSVParser csvParser = new CSVParserBuilder()
.withSeparator(";".charAt(0))
.withQuoteChar(QUOTE)
.build();
CSVReader csvReader = new CSVReader(reader);
I faced with the following problem. When I pass to csvParser.parseLine(currentLine)-method line with multiple semicolons at the end of csv-line (for example):
"GGDIRECT";"ARCIT_TEST_1";"Test table for ArcIt";500;;;
it returns exception:
java.io.IOException: Unterminated quoted field at end of CSV line
It looks like you haven't used csvParser
because you don't pass it to the CSVReader
constructor. This means you parse with the default field separator and not ";".
I'm not near PC to check but see if another constructor accepts reader
and csvParser
, something like :
CSVReader csvReader = new CSVReader(reader, linesToSkip, csvParser);
EDIT
It looks like I have been viewing incorrect website for CSVReader
API. However you have found the correct way to link your customised CSVParser
with Reader
to make CSVReader
:
CSVParser csvParser = new CSVParserBuilder().withSeparator(';')
.withQuoteChar('\"')
.build();
CSVReader csvReader = new CSVReaderBuilder(reader).withCSVParser(csvParser).build();
You can verify with test:
String content = "\"GGDIRECT\";\"ARCIT_TEST_1\";\"Test table for ArcIt\";500;;;";
try (CSVReader csvReader = new CSVReader(new StringReader(content))) {
csvReader.forEach(arr -> System.out.format("WRONG String[%d] = %s%n", arr.length, Arrays.toString(arr)));
}
try (CSVReader csvReader = new CSVReaderBuilder(new StringReader(content)).withCSVParser(csvParser).build()) {
csvReader.forEach(arr -> System.out.format("RIGHT String[%d] = %s%n", arr.length, Arrays.toString(arr)));
}
Prints:
WRONG String[1] = [GGDIRECT";"ARCIT_TEST_1";"Test table for ArcIt";500;;;]
RIGHT String[7] = [GGDIRECT, ARCIT_TEST_1, Test table for ArcIt, 500, , , ]