private JsonNode readCsvFile(URI fileUrl) { ArrayNode resultNode = GlobalObjectMapper.getDefaultMapper().createArrayNode(); ICsvMapReader mapReader = null; try { mapReader = new CsvMapReader(new FileReader(new File(fileUrl)), CsvPreference.STANDARD_PREFERENCE); // the header columns are used as the keys to the Map String[] headers = getHeaders(); CellProcessor[] processors = getProcessors(); Map<String, Object> contentsMap; while( (contentsMap = mapReader.read(headers, processors)) != null ) { LOGGER.debug(String.format("lineNo=%s, rowNo=%s, customerMap=%s", mapReader.getLineNumber(), mapReader.getRowNumber(), contentsMap)); resultNode.add(GlobalObjectMapper.getDefaultMapper().convertValue(contentsMap, JsonNode.class)); } } catch (Exception e) { LOGGER.error("Fail to read result file : {}", e.getMessage()); throw new RuntimeException("Fail to read result file."); } finally { if( mapReader != null ) { try { mapReader.close(); } catch (IOException e) {} } } return resultNode; }
public List<Map<String, Object>> readCsv(String fileName, List<Field> fieldList, int index, int length) { ICsvMapReader mapReader = null; List<Map<String, Object>> returnList = new ArrayList<>(); try { mapReader = new CsvMapReader(new FileReader(fileName) , new CsvPreference.Builder('"', ',', "\r\n") .ignoreEmptyLines(false) .build()); // the header columns are used as the keys to the Map final String[] header = mapReader.getHeader(true); Map<String, Object> rowMap; while( (rowMap = mapReader.read(header, getProcessors(fieldList))) != null && mapReader.getRowNumber() - 1 < index + length + 1) { if(mapReader.getRowNumber() - 1 > index){ returnList.add(rowMap); } } } catch (FileNotFoundException e){ throw new WorkbenchException(CSV_FILE_NOT_FOUND, "CSV File Not Founded.", e); } catch (IOException e){ throw new WorkbenchException(CSV_FILE_NOT_FOUND, "read CSV IOException.", e); } finally { try { if(mapReader != null) mapReader.close(); } catch (Exception e){} } return returnList; }