I need to get access to the number of rows for a CSV being processed but it seems Super CSV does not have an out-of-the-box option.
The code below seems to do the trick:
private int getCsvFileTotalRows(InputStream inputStream) throws IOException {
int totalRows = 0;
BOMInputStream bomInputStream = new BOMInputStream(inputStream, BOMS);
Reader fileReader = new BufferedReader(new InputStreamReader(bomInputStream, StandardCharsets.UTF_8));
CsvMapReader csvMapReader = new CsvMapReader(fileReader, CsvPreference.EXCEL_PREFERENCE);
String[] headers = csvMapReader.getHeader(true);
while (csvMapReader.read(headers) != null) {
totalRows++;
}
return totalRows;
}
But the problem is that there is already code in place which is creating a CsvMapReader based on an inputStream to process the file, so adding the method above does not work since the CsvMapReader instance was already created with the same inputStream instance I am trying to use in the method above.
How can I achieve this with Super CSV?