@Test public final void testManual() throws IOException { ICsvListReader reader = new CsvListReader(new StringReader(CSV), CsvPreference.STANDARD_PREFERENCE); reader.getHeader(true); // skip header List<String> firstNames = new ArrayList<String>(); List<String> line; while((line = reader.read()) != null){ firstNames.add(line.get(0)); } // prints: [John, Bob] System.out.println(firstNames); }
protected List<String> readInternal() throws Exception { if (readed){ return current; } current= csvReader.read(); readed=true; return current; }
CsvPreference.STANDARD_PREFERENCE); listReader.getHeader(true); // skip the header while( (row = listReader.read(processors)) != null ) { System.out.println(String.format("lineNo=%s, rowNo=%s, row=%s", listReader.getLineNumber(), listReader.getRowNumber(), row)); listReader.close();
sr = new StringReader(mapping); listReader = new CsvListReader(sr,CsvPreference.STANDARD_PREFERENCE); csvList = (ArrayList<String>)listReader.read(); } catch (IOException e) { logger.error("Cannot read the mapping string", e); try { sr.close(); listReader.close(); } catch (IOException e) { logger.error("Error closing Csv reader", e);
@Override public List<SalaryYear> read(InputStream is) throws Exception { List<SalaryYear> elements = new ArrayList<SalaryYear>(); ICsvListReader reader = new CsvListReader(new InputStreamReader(is), CsvPreference.EXCEL_PREFERENCE); try { reader.getHeader(true); List<String> element; while ((element = reader.read()) != null) { String[] tokens = element.toArray(new String[element.size()]); SalaryYear salaryYear = new SalaryYear(); salaryYear.setEmail(tokens[0]); salaryYear.setYear(2013); List<SalaryMonth> months = new ArrayList<SalaryMonth>(12); for (int i = 1; i < 13; i++) { months.add(buildSalaryMonth(i, tokens[i])); } salaryYear.setMonths(months); elements.add(salaryYear); } } finally { reader.close(); } return elements; }
csvReader.getHeader(true); // skips the header column while ((line = csvReader.read(processors))!=null) { int lineNo = csvReader.getLineNumber(); for (int colNo = 0 ; colNo < line.size() ; colNo++) { CSVEntry csvEntry = new CSVEntry((String) line.get(colNo), lineNo, colNo+1);
/** * Construct CsvIterator instance. * * @param reader reader to use * @param header an array of attribute names, if there is no header row * in the file, or null to read attribute names from the * header row */ public CsvIterator(ICsvListReader reader, String[] header) { try { m_reader = reader; m_header = header != null ? header : reader.getHeader(false); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void teardown() { super.teardown(); try { lineSr.close(); lineListReader.close(); } catch (IOException e) { logger.error("Cannot close the readers", e); DTThrowable.rethrow(e); } }
@Override protected void set(List<String> val, Row row) throws Exception { row.setDataSourceInfo(filename); row.setDataSourceRowNumber(csvReader.getLineNumber()); int i=0; if (val==null){
public class CSVReader extends DataReader<List<String>> { private final ICsvListReader reader; ... @Override public List<String> readNext() throws IOException { return reader.read(); //read already throws an IOException so you're good to go } }
sr = new StringReader(mappingString); listReader = new CsvListReader(sr,CsvPreference.STANDARD_PREFERENCE); csvList = (ArrayList<String>) listReader.read(); } catch (IOException e) { logger.error("Cannot read the mapping string", e); try { sr.close(); listReader.close(); } catch (IOException e) { logger.error("Error closing Csv reader", e);
@Override public void teardown() { super.teardown(); try { lineSr.close(); lineListReader.close(); } catch (IOException e) { logger.error("Cannot close the readers", e); DTThrowable.rethrow(e); } }
private static final String CSV = "firstName,lastName\nJohn,Dunbar\nBob,Down"; @Test public final void testCollector() throws IOException { ICsvListReader reader = new CsvListReader(new StringReader(CSV), CsvPreference.STANDARD_PREFERENCE); reader.getHeader(true); // skip header // Collector processor 'collects' values from a column List<Object> firstNames = new ArrayList<Object>(); CellProcessor[] processors = {new Collector(firstNames), null}; while(reader.read(processors) != null){ // just keep reading - Collector will collect the values } // prints: [John, Bob] System.out.println(firstNames); }
/** * Returns true if there are more items to read, false otherwise. * * @return true if there are more items to read, false otherwise */ public boolean hasNext() { try { m_currentLine = m_reader.read(); } catch (IOException e) { throw new RuntimeException(e); } return m_currentLine != null; }
sr=new StringReader(mapping); listReader = new CsvListReader(sr,CsvPreference.STANDARD_PREFERENCE); csvList = (ArrayList<String>) listReader.read(); } catch (IOException e) { logger.error("Cannot read the mapping string", e); try { sr.close(); listReader.close(); } catch (IOException e) { logger.error("Error closing Csv reader", e);
@Override public void teardown() { super.teardown(); try { lineSr.close(); lineListReader.close(); } catch (IOException e) { logger.error("Cannot close the readers", e); DTThrowable.rethrow(e); } }
try (ICsvListReader reader = new CsvListReader(new InputStreamReader(is), prefs)) { Map<Integer, Integer> colMap = new HashMap<>(); String[] header = reader.getHeader(true); List<String> substationIds = new ArrayList<>(); for (int col = 0; col < header.length; col++) { int row = 0; List<String> line; while ((line = reader.read()) != null) { for (int col = 0; col < line.size(); col++) { Integer newCol = colMap.get(col);
@Test public void testNullsWithDefaultBehaviour() throws IOException{ String csv = ",null,NULL\n"; try (ICsvListReader reader = new CsvListReader(new StringReader(csv), CsvPreference.STANDARD_PREFERENCE)){ List<String> values = reader.read(); for (String v : values){ System.out.println(v == null); } } }
sr = new StringReader(mappingString); listReader = new CsvListReader(sr,CsvPreference.STANDARD_PREFERENCE); csvList = (ArrayList<String>)listReader.read(); } catch (IOException e) { logger.error("Cannot read the mapping string", e); try { sr.close(); listReader.close(); } catch (IOException e) { logger.error("Error closing Csv reader", e);