CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(delimiter.charAt(0)).withEscape(lineEnding.charAt(0));
if (StringUtils.isNotEmpty(enclosure)) {
csvFormat = csvFormat.withQuote(enclosure.charAt(0));
} else {
csvFormat = csvFormat.withQuoteMode(NONE);
}
static void processFile(final File file) {
FileReader filereader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(filereader);
bufferedReader.readLine();// try-catch omitted
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
CSVParser parser = new CSVParser(bufferedReader, format);
final List<CSVRecord> records = parser.getRecords();
//stuff
}
CSVParser csvParser = CSVFormat.DEFAULT.withDelimiter(',').withQuote('"').parse(new StringReader(
"\"[\"\"54bb051e-3d12-11e5-91cd-b8f6b11b7feb\"\",\"\"472a9748-3d12-11e5-91cd-b8f6b11b7feb\"\"]\",Hallo,114058,Leon,31,\" \",8400,bar,FOO"));
System.out.println(csvParser.getRecords().get(0).get(0));
private void parse() throws Exception {
FileReader reader = new FileReader("example.csv");
reader.readLine(); // Read the first/current line.
Iterable <CSVRecord> records = CSVFormat.EXCEL.withQuote('"').withDelimiter(';').parse(reader);
for (CSVRecord csvRecord: records) {
// do something
}
}
public void testWithDelimiter() throws Exception {
final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
assertEquals('!', formatWithDelimiter.getDelimiter());
}
public void testApacheCommonCsvDelimiter() throws Exception {
StringReader stringReader = new StringReader("foo;bar;15;true");
CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(';').withHeader("firstName", "lastName", "age", "married");
ApacheCommonCsvRecord record = getApacheCommonCsvRecord(stringReader, csvFormat);
GenericRecord<Foo> actual = mapper.processRecord(record);
Foo foo = actual.getPayload();
assertThat(foo).isNotNull();
assertThat(foo.getFirstName()).isEqualTo("foo");
assertThat(foo.getLastName()).isEqualTo("bar");
assertThat(foo.getAge()).isEqualTo(15);
assertThat(foo.isMarried()).isTrue();
}
public DataGenerator getGenerator(OutputStream os) throws IOException {
CSVFormat csvFormat = getSettings().getMode(CsvMode.class).getFormat();
if (getSettings().getMode(CsvMode.class) == CsvMode.CUSTOM) {
csvFormat = CSVFormat.DEFAULT.withDelimiter((char) getSettings().getConfig(DelimitedDataConstants.DELIMITER_CONFIG)).withEscape((char) getSettings().getConfig(DelimitedDataConstants.ESCAPE_CONFIG)).withQuote((char) getSettings().getConfig(DelimitedDataConstants.QUOTE_CONFIG));
}
return new DelimitedCharDataGenerator(createWriter(os), csvFormat, header, headerKey, valueKey, replaceNewLines ? replaceNewLinesString : null);
}
private CSVFormat getFormat() {
CSVFormat format = getFormatFromConfig().withDelimiter(getDelimiterFromConfig());
String[] header = getHeaderFromConfig();
if (header != null) {
format = format.withHeader(header);
} else {
format = format.withHeader();
}
return format;
}
protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape, final Character quote, final QuoteMode quoteMode, final String nullString, final String recordSeparator) {
CSVFormat csvFormat = CSVFormat.valueOf(format);
if (delimiter != null) {
csvFormat = csvFormat.withDelimiter(delimiter);
}
if (escape != null) {
csvFormat = csvFormat.withEscape(escape);
}
if (quote != null) {
csvFormat = csvFormat.withQuote(quote);
}
if (quoteMode != null) {
csvFormat = csvFormat.withQuoteMode(quoteMode);
}
if (nullString != null) {
csvFormat = csvFormat.withNullString(nullString);
}
if (recordSeparator != null) {
csvFormat = csvFormat.withRecordSeparator(recordSeparator);
}
return csvFormat;
}
private DataParser createParser(String id, OverrunReader reader, long offset) throws DataParserException {
Utils.checkState(reader.getPos() == 0, Utils.formatL("reader must be in position '0', it is at '{}'", reader.getPos()));
CSVFormat csvFormat = getSettings().getMode(CsvMode.class).getFormat();
if (getSettings().getMode(CsvMode.class) == CsvMode.CUSTOM) {
csvFormat = CSVFormat.DEFAULT.withDelimiter((char) getSettings().getConfig(DelimitedDataConstants.DELIMITER_CONFIG)).withEscape((char) getSettings().getConfig(DelimitedDataConstants.ESCAPE_CONFIG)).withQuote((char) getSettings().getConfig(DelimitedDataConstants.QUOTE_CONFIG));
}
try {
return new DelimitedCharDataParser(getSettings().getContext(), id, reader, offset, (Integer) getSettings().getConfig(DelimitedDataConstants.SKIP_START_LINES), csvFormat, getSettings().getMode(CsvHeader.class), getSettings().getMaxRecordLen(), getSettings().getMode(CsvRecordType.class));
} catch (IOException ex) {
throw new DataParserException(Errors.DELIMITED_PARSER_00, id, offset, ex.toString(), ex);
}
}