Reputation: 2237
How to Ignore empty lines? I am using the below snippet of code and it doesnt ignore the empty lines. Any pointer configuration available in CSV parser to fix this issue?
public CSVParser parseCSV(InputStream inputStream) {
try {
return new CSVParser(new InputStreamReader(inputStream, StandardCharsets.UTF_8), CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withSkipHeaderRecord()
.withIgnoreEmptyLines()
.withTrim());
} catch (IOException e) {
throw new IPRSException(e);
}
}
Sample file
h1,h2,h3
d1,d2,d3
,,,
Expected output
d1,d2,d3
Upvotes: 1
Views: 1807
Reputation: 2237
Apache CSV parser doesnt support empty lines out of the box, so ended up writing custom code.
private boolean isEmpty(CSVRecord csvRecord){
if (null == csvRecord) return true;
for (int i = 0; i < csvRecord.size(); i++) {
if (StringUtils.isNotBlank(csvRecord.get(i))) {
return false;
}
}
return true;
}
public List<Map<String, Object>> getMapFromCSV(InputStream inputStream) {
try {
CSVParser parser = parseCSV(inputStream);
return getMap(parser.getRecords().stream()
.sequential().filter(v -> !isEmpty(v))
.collect(Collectors.toList()), parser.getHeaderMap());
} catch (IOException e) {
throw new Exception(e);
}
}
private List<Map<String, Object>> getMap (List<CSVRecord> records, Map<String, Integer> headers) {
Map<Integer, String> headerMap = formatHeaderMap(headers);
List<Map<String, Object>> data = new ArrayList<>();
for (int i = 1; i < records.size(); i++) {
Map<String, Object> map = new HashMap<>();
try {
CSVRecord record = records.get(i);
for (int j = 0; j < record.size(); j++) {
map.put(headerMap.get(j), record.get(j));
}
data.add(map);
} catch (Exception e) {
throw new Exception(e);
}
}
return data;
}
private Map<Integer, String> formatHeaderMap(Map<String, Integer> map) {
Map<Integer, String> data = new HashMap<>();
map.forEach((k , v) -> data.put(v, inputSanitizerForUtf8(k)));
return data;
}
Upvotes: 2