private static CsvPOJO readWithCsvMapReader(String file) throws Exception { CsvPOJO csvPojo = new CsvPOJO(); Map<String, String> readMap = null; ICsvMapReader mapReader = null; try { mapReader = new CsvMapReader(new FileReader(file), CsvPreference.STANDARD_PREFERENCE); String[] headers = mapReader.getHeader(true); List<CsvRow> rows = new ArrayList<CsvRow>(); while ((readMap = mapReader.read(headers)) != null) { CsvRow row = new CsvRow(); List<String> columns = new ArrayList<String>(); for (String h : headers) { if (!StrUtil.isEmpty(h)) { columns.add(readMap.get(h)); } } row.setCols(columns); rows.add(row); } csvPojo.setHeaders(headers); csvPojo.setRows(rows); } finally { if (mapReader != null) { mapReader.close(); } } return csvPojo; }
protected List<Map<String, Object>> readCsvFile(URI fileUrl) { List<Map<String, Object>> result = Lists.newArrayList(); 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 ) { result.add(contentsMap); } } 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) {} } } LOGGER.info("Query Result Count : " + result.size()); return result; }
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; }
if (csvMapReader != null) { try { csvMapReader.close(); } catch (IOException e) { LOGGER.error("Error closing CSV map reader: " + e.getMessage());
sr.addResponse(questionResponse); reader.close(); return responses; } catch (IOException io) {
sr.getNonCustomResponses().add(questionResponse); reader.close(); return responses; } catch (IOException io) {
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; }