private void init() throws IOException { _parser = _format.parse(RecordReaderUtils.getFileReader(_dataFile)); _iterator = _parser.iterator(); }
public static String[] parseAsCsv(String key, String value, Function<String, String> valueProcessor) { String cleanValue = MultivalueProperty.trimFieldsAndRemoveEmptyFields(value); List<String> result = new ArrayList<>(); try (CSVParser csvParser = CSVFormat.RFC4180 .withHeader((String) null) .withIgnoreEmptyLines() .withIgnoreSurroundingSpaces() .parse(new StringReader(cleanValue))) { List<CSVRecord> records = csvParser.getRecords(); if (records.isEmpty()) { return ArrayUtils.EMPTY_STRING_ARRAY; } processRecords(result, records, valueProcessor); return result.toArray(new String[result.size()]); } catch (IOException e) { throw new IllegalStateException("Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e); } }
try (final InputStream is = new FileInputStream(csvFile)) { try (final InputStreamReader reader = new InputStreamReader(is, charset)) { final Iterable<CSVRecord> records = csvFormat.withFirstRecordAsHeader().parse(reader); for (final CSVRecord record : records) { final String key = record.get(lookupKeyColumn);
try (final InputStream is = new FileInputStream(csvFile)) { try (final InputStreamReader reader = new InputStreamReader(is, charset)) { final CSVParser records = csvFormat.withFirstRecordAsHeader().parse(reader); RecordSchema lookupRecordSchema = null; for (final CSVRecord record : records) {
public GrblLookups(String prefix) { String filename = prefix + "_" + Localization.loadedLocale() + ".csv"; URL u = GrblLookups.class.getResource(pathFor(filename)); if (u == null) { filename = prefix + "_en_US.csv"; } try { try (BufferedReader reader = new BufferedReader( new InputStreamReader( GrblLookups.class.getResourceAsStream(pathFor(filename))))) { Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(reader); for (CSVRecord record : records) { List<String> list = Lists.newArrayList(record.iterator()); lookups.put(record.get(0), list.toArray(new String[0])); } } } catch (IOException ex) { System.out.println("Unable to load GRBL resources."); ex.printStackTrace(); } }
@Test public void testParseCustomNullValues() throws IOException { final StringWriter sw = new StringWriter(); final CSVFormat format = CSVFormat.DEFAULT.withNullString("NULL"); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { printer.printRecord("a", null, "b"); } final String csvString = sw.toString(); assertEquals("a,NULL,b" + recordSeparator, csvString); try (final CSVParser iterable = format.parse(new StringReader(csvString))) { final Iterator<CSVRecord> iterator = iterable.iterator(); final CSVRecord record = iterator.next(); assertEquals("a", record.get(0)); assertEquals(null, record.get(1)); assertEquals("b", record.get(2)); assertFalse(iterator.hasNext()); } }
final Reader in = new StringReader("7080001, XI, ProvinceX, TownX, BRGX, \"SHOOL, BRGX\", \"0054A,0055A,0055B,0055C\" "); final CSVFormat csvFileFormat = CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(); for (CSVRecord record: csvFileFormat.parse(in)) { for (String field: record) { System.out.println("\"" + field + "\""); } System.out.println(); }
protected CSVRecord getListFileCsvRecord(String listFileLine) { try { return CSVFormat.EXCEL.parse(new StringReader(listFileLine)).iterator().next(); } catch (IOException e) { throw new RuntimeException("error parsing list file CSV record: " + listFileLine, e); } }
public TransactionLevelFeeReport(String urlValue) throws IOException, ParseException { if (urlValue == null || "".equals(urlValue)) { this.valid = false; return; } this.valid = true; URL url = new URL(urlValue); InputStreamReader is = new InputStreamReader(url.openStream()); for (CSVRecord record : CSVFormat.EXCEL.withFirstRecordAsHeader().parse(is)) { rows.add(new TransactionLevelFeeReportRow(record)); } }
public TransactionLevelFeeReport(String urlValue) throws IOException, ParseException { if (urlValue == null || "".equals(urlValue)) { this.valid = false; return; } this.valid = true; URL url = new URL(urlValue); InputStreamReader is = new InputStreamReader(url.openStream()); for (CSVRecord record : CSVFormat.EXCEL.withFirstRecordAsHeader().parse(is)) { rows.add(new TransactionLevelFeeReportRow(record)); } }
private CSVParser(Reader input) { try { this.parser = CSVFormat.RFC4180.parse(input); this.iterator = parser.iterator() ; } catch (IOException e) { throw new CSVParseException("Failed to create the CSV parser: " + e.getMessage(), e); } }
public List<KeyPairs> fromCSV(InputStream inputStream) throws Exception { Reader reader = new InputStreamReader(inputStream); Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(reader); Iterable<CSVRecord> iterable = () -> records.iterator(); Stream<CSVRecord> stream = StreamSupport.stream(iterable.spliterator(), false); return stream.map((entry) -> { String ethAddress = entry.get(0); String btcAddress = entry.get(1); LOG.info("Reading: ETH={} BTC={}", ethAddress, btcAddress); return new KeyPairs(btcAddress, ethAddress, true); }).collect(Collectors.toList()); }
try (final CSVParser parser = format.parse(getTestInput())) { for (final CSVRecord csvRecord : parser) { records++;
public static String parseCsv(File file, String... format) { try { CSVParser parse = CSVFormat.DEFAULT .withSkipHeaderRecord(CONFIG.shouldSkipFirstLine) .withHeader(format) .parse(new FileReader(file)); return RecordToXMLSerializer.getFromReader(parse); } catch (IOException e) { throw new IllegalStateException("Received IOException while parsing file.", e); } }
@Override public Map<String, Object> next() throws IOException { if (csvParser == null) { csvParser = CSVFormat.DEFAULT.withHeader().withDelimiter(delimiter).parse(inputStream); iterator = csvParser.iterator(); } if (!iterator.hasNext()) { return null; } CSVRecord csvRecord = iterator.next(); return new HashMap<String, Object>(csvRecord.toMap()); }
public static String parse(Reader reader, String... format) { try { CSVParser parse = CSVFormat.DEFAULT .withSkipHeaderRecord(CONFIG.shouldSkipFirstLine) .withHeader(format) .parse(reader); return RecordToXMLSerializer.getFromReader(parse); } catch (IOException e) { throw new IllegalStateException("Received IOException while parsing file.", e); } }
@Override public Map<String, String> next() throws IOException { if (csvParser == null) { csvParser = CSVFormat.DEFAULT.withHeader().withDelimiter(delimiter).parse(inputStream); iterator = csvParser.iterator(); } if (!iterator.hasNext()) { return null; } CSVRecord csvRecord = iterator.next(); return csvRecord.toMap(); }
static void testOpTraceLogFile(final File logFile, final Consumer<CSVRecord> csvRecordTestFunc) throws IOException { try (final BufferedReader br = new BufferedReader(new FileReader(logFile))) { try (final CSVParser csvParser = CSVFormat.RFC4180.parse(br)) { csvParser.forEach(csvRecordTestFunc); } } }
@Override public void open(final Serializable checkpoint) throws Exception { final CSVFormat csvFormat = newFormat(); parser = csvFormat.parse(newReader()); iterator = new IteratorReader<CSVRecord>(parser.iterator()); mapperInstance = mapper == null ? new BeanLocator.LocatorInstance<CsvReaderMapper>( mapping != null ? new DefaultMapper(Thread.currentThread().getContextClassLoader().loadClass(mapping)) : NOOP_MAPPER, null) : BeanLocator.Finder.get(locator).newInstance(CsvReaderMapper.class, mapper); super.open(checkpoint); }
protected void parseCSV(Reader in, String[] header, PerformanceReport report) throws IOException { CSVFormat csvFormat = CSVFormat.newFormat(delimiter).withHeader(header).withQuote('"').withSkipHeaderRecord(); Iterable<CSVRecord> records = csvFormat.parse(in); for (CSVRecord record : records) { final HttpSample sample = getSample(record); try { report.addSample(sample); } catch (SAXException e) { throw new IllegalStateException("Error parsing file '" + report.getReportFileName() + "': Unable to add sample for CSVRecord " + record, e); } } }