protected List<String> readInternal() throws Exception { if (readed){ return current; } current= csvReader.read(); readed=true; return current; }
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 } }
/** * 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; }
@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); } } }
ICsvListReader reader = new CsvListReader(new FileReader("weird.csv"), CsvPreference.Builder('\t', ',', "\r\n").build() ); List<String> record = reader.read(); for(String value : record) System.out.println(value);
@Test public void testNullsUsingProcessors() throws IOException{ String csv = ",null,NULL\n"; CellProcessor[] processors = {new CustomOptional(), new CustomOptional(), new CustomOptional()}; try (ICsvListReader reader = new CsvListReader(new StringReader(csv), CsvPreference.STANDARD_PREFERENCE)){ List<Object> values = reader.read(processors); for (Object v : values){ System.out.println(v == null); } } }
@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); }
public Put parseLine(String s) { Put put = null; try { lineSr.open(s); csvLineList = (ArrayList<String>) lineListReader.read(); } catch (IOException e) { logger.error("Cannot read the property string", e); DTThrowable.rethrow(e); } put = new Put(csvLineList.get(rowIndex).getBytes()); for (Entry<Integer, ColDef> e : colMap.entrySet()) { String colValue = csvLineList.get(e.getKey()); put.add(e.getValue().colFam.getBytes(),e.getValue().colName.getBytes(), colValue.getBytes()); } csvLineList.clear(); return put; }
public Put parseLine(String s) { Put put = null; try { lineSr.open(s); csvLineList = (ArrayList<String>)lineListReader.read(); } catch (IOException e) { logger.error("Cannot read the property string", e); DTThrowable.rethrow(e); } put = new Put(csvLineList.get(rowIndex).getBytes()); for (Entry<Integer, ColDef> e : colMap.entrySet()) { String colValue = csvLineList.get(e.getKey()); put.add(e.getValue().colFam.getBytes(),e.getValue().colName.getBytes(), colValue.getBytes()); } csvLineList.clear(); return put; }
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); }
try { lineSr.open(s); csvLineList = (ArrayList<String>) lineListReader.read(); } catch (IOException e) { logger.error("Cannot read the property string", e);
public FetchTableRowItem(String path, String tableName) { super(true); try { this.tableName = tableName; this.table = new Table(); this.table.setName(tableName); listReader = new CsvListReader( new FileReader(path + tableName + ".csv"), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE ); List<String> list = listReader.read(); for (String columnName : list) { table.getColumns().add(new Column(columnName)); } } catch (FileNotFoundException ex) { Logger.getLogger(FetchTableRowItem.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(FetchTableRowItem.class.getName()).log(Level.SEVERE, null, ex); } }
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);
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);
while ((row = listReader.read()) != null) {
@Override public RecordSet fetchResultRecordSet() { try { List<String> list = listReader.read(); if (list != null && !list.isEmpty()) { RecordSet rs = new RecordSet(); rs.setColumns(table.getColumns()); rs.addRow(Row.create(list)); return rs; } } catch (IOException e) { System.out.println("Error"); } return null; }
@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; }
while( (row = listReader.read(processors)) != null ) { System.out.println(String.format("lineNo=%s, rowNo=%s, row=%s", listReader.getLineNumber(), listReader.getRowNumber(), row));
private static void format(InputStream is, boolean zipped, PrintStream out) throws IOException { Table table; CsvPreference prefs = new CsvPreference.Builder('"', ',', "\r\n").build(); try (ICsvListReader reader = new CsvListReader(createReader(is, zipped), prefs)) { String[] header = reader.getHeader(true); table = new Table(header.length, BorderStyle.CLASSIC_WIDE); for (String cell : header) { table.addCell(cell); } List<String> row; while ((row = reader.read()) != null) { for (int i = 0; i < row.size(); i++) { String cell = row.get(i); if (header[i].equals(HistoDbMetaAttributeId.datetime.toString())) { DateTime datetime = new DateTime(Long.parseLong(cell) * 1000L); table.addCell(datetime.toString()); } else { table.addCell(cell); } } } } out.println(table.render()); }
@Override protected void initializeColumn() throws Exception { List<String> list=csvReader.read(); if (list==null){ return; } columns=CommonUtils.list(); if (CommonUtils.isEmpty(table.getColumns())){ for(String text:list){ Column column=new Column(text); columns.add(column); table.getColumns().add(column); } } else{ for(String columnName:list){ Column column=table.getColumns().get(columnName); if (column==null){ table.getColumns().get(columnName.toLowerCase()); } if (column==null){ table.getColumns().get(columnName.toUpperCase()); } if (column!=null){ columns.add(column); } } } }