public WriteCSVResult(final CSVFormat csvFormat, final RecordSchema recordSchema, final SchemaAccessWriter schemaWriter, final OutputStream out, final String dateFormat, final String timeFormat, final String timestampFormat, final boolean includeHeaderLine, final String charSet) throws IOException { super(out); this.recordSchema = recordSchema; this.schemaWriter = schemaWriter; this.dateFormat = dateFormat; this.timeFormat = timeFormat; this.timestampFormat = timestampFormat; this.includeHeaderLine = includeHeaderLine; final CSVFormat formatWithHeader = csvFormat.withSkipHeaderRecord(true); final OutputStreamWriter streamWriter = new OutputStreamWriter(out, charSet); printer = new CSVPrinter(streamWriter, formatWithHeader); fieldValues = new Object[recordSchema.getFieldCount()]; }
public CSVRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema, final CSVFormat csvFormat, final boolean hasHeader, final boolean ignoreHeader, final String dateFormat, final String timeFormat, final String timestampFormat, final String encoding) throws IOException { super(logger, schema, hasHeader, ignoreHeader, dateFormat, timeFormat, timestampFormat); final Reader reader = new InputStreamReader(new BOMInputStream(in), encoding); CSVFormat withHeader; if (hasHeader) { withHeader = csvFormat.withSkipHeaderRecord(); if (ignoreHeader) { withHeader = withHeader.withHeader(schema.getFieldNames().toArray(new String[0])); } else { withHeader = withHeader.withFirstRecordAsHeader(); } } else { withHeader = csvFormat.withHeader(schema.getFieldNames().toArray(new String[0])); } csvParser = new CSVParser(reader, withHeader); }
/** * Returns a new {@code CSVFormat} with skipping the header record set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified skipHeaderRecord setting. * @see #withSkipHeaderRecord(boolean) * @see #withHeader(String...) * @since 1.1 */ public CSVFormat withSkipHeaderRecord() { return this.withSkipHeaderRecord(true); }
/** * Returns a new {@code CSVFormat} using the first record as header. * * <p> * Calling this method is equivalent to calling: * </p> * * <pre> * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord(); * </pre> * * @return A new CSVFormat that is equal to this but using the first record as header. * @see #withSkipHeaderRecord(boolean) * @see #withHeader(String...) * @since 1.3 */ public CSVFormat withFirstRecordAsHeader() { return withHeader().withSkipHeaderRecord(); }
@Test public void testSkipHeaderRecordFalse() throws IOException { // functionally identical to testHeader, used to test CSV-153 final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3").withSkipHeaderRecord(false))) { printer.printRecord("a", "b", "c"); printer.printRecord("x", "y", "z"); assertEquals("C1,C2,C3\r\na,b,c\r\nx,y,z\r\n", sw.toString()); } }
@Test public void testSkipHeaderRecordTrue() throws IOException { // functionally identical to testHeaderNotSet, used to test CSV-153 final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null).withHeader("C1", "C2", "C3").withSkipHeaderRecord(true))) { printer.printRecord("a", "b", "c"); printer.printRecord("x", "y", "z"); assertEquals("a,b,c\r\nx,y,z\r\n", sw.toString()); } }
/** * Sets skipping the header record to {@code true}. * * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting. * @see #withSkipHeaderRecord(boolean) * @see #withHeader(String...) * @since 1.1 */ public CSVFormat withSkipHeaderRecord() { return this.withSkipHeaderRecord(true); }
/** * Returns a new {@code CSVFormat} with skipping the header record set to {@code true}. * * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting. * @see #withSkipHeaderRecord(boolean) * @see #withHeader(String...) * @since 1.1 */ public CSVFormat withSkipHeaderRecord() { return this.withSkipHeaderRecord(true); }
/** * Returns a new {@code CSVFormat} with skipping the header record set to {@code true}. * * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting. * @see #withSkipHeaderRecord(boolean) * @see #withHeader(String...) * @since 1.1 */ public CSVFormat withSkipHeaderRecord() { return this.withSkipHeaderRecord(true); }
@Override public List<TestScenarioResult> records() { try (Reader bufferedReader = new BufferedReader(new InputStreamReader(checkNotNull(getClass().getResourceAsStream(fileName), fileName + " could not be found")))) { return CSVFormat.DEFAULT .withHeader(headers) .withSkipHeaderRecord(true) .parse(bufferedReader) .getRecords().stream() .map(TestScenarioResult::createFromCSV).collect(Collectors.toList()); } catch (Exception e) { throw new RuntimeException(fileName + " could not be opened", e); } }
/** * Returns a new {@code CSVFormat} using the first record as header. * * <p> * Calling this method is equivalent to calling: * </p> * * <pre> * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord(); * </pre> * * @return A new CSVFormat that is equal to this but using the first record as header. * @see #withSkipHeaderRecord(boolean) * @see #withHeader(String...) * @since 1.3 */ public CSVFormat withFirstRecordAsHeader() { return withHeader().withSkipHeaderRecord(); }
@Override public List<TestScenarioResult> records() { try (Reader bufferedReader = new BufferedReader(new InputStreamReader(checkNotNull(getClass().getResourceAsStream(fileName), fileName + " could not be found")))) { return CSVFormat.DEFAULT .withHeader(headers) .withSkipHeaderRecord(true) .parse(bufferedReader) .getRecords().stream() .map(TestScenarioResult::createFromCSV).collect(Collectors.toList()); } catch (Exception e) { throw new RuntimeException(fileName + " could not be opened", e); } }
/** * Returns a new {@code CSVFormat} using the first record as header. * * <p> * Calling this method is equivalent to calling: * </p> * * <pre> * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord(); * </pre> * * @return A new CSVFormat that is equal to this but using the first record as header. * @see #withSkipHeaderRecord(boolean) * @see #withHeader(String...) * @since 1.3 */ public CSVFormat withFirstRecordAsHeader() { return withHeader().withSkipHeaderRecord(); }
format = format.withQuoteMode(QuoteMode.ALL); format = format.withRecordSeparator('\n'); format = format.withSkipHeaderRecord(false);
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); } }
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); } }
private UserAgentParser parse(final Reader input) throws IOException, ParseException { final List<Rule> rules = new ArrayList<>(); final CSVFormat format = CSVFormat.DEFAULT.withSkipHeaderRecord(); try (CSVParser records = format.parse(input)) { for (final CSVRecord record : records) { final Rule rule = getRule(record); if (rule != null) { rules.add(rule); } } } return new UserAgentParserImpl(rules.toArray(new Rule[0]), getDefaultCapabilities()); }
private UserAgentParser parse(final Reader input) throws IOException, ParseException { final List<Rule> rules = new ArrayList<>(); final CSVFormat format = CSVFormat.DEFAULT.withSkipHeaderRecord(); try (CSVParser records = format.parse(input)) { for (final CSVRecord record : records) { final Rule rule = getRule(record); if (rule != null) { rules.add(rule); } } } return new UserAgentParserImpl(rules.toArray(new Rule[0]), getDefaultCapabilities()); }
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); } } }
@Override public void setFromArgs(Namespace args) throws CommandLineParsingException { super.setFromArgs(args); csv = args.getString("input"); format = ((CSVFormat.Predefined) args.get("type")).getFormat(); chr = args.getInt("chr") - 1; pos = args.getInt("pos") - 1; ref = args.getInt("ref") - 1; alt = args.getInt("alt") - 1; header = args.getBoolean("header"); if ( header) format = format.withFirstRecordAsHeader().withSkipHeaderRecord(); }