public CSVFormat getCSVFormat() { return CSVFormat.DEFAULT .withDelimiter(delimiter) .withRecordSeparator(recordSeparator) .withNullString(nullString) .withEscape(escapeCharacter) .withQuote(quoteCharacter) .withQuoteMode(quoteMode); }
private List<Measurement> readMeasurements(String measurementCsvLocation) { List<Measurement> measurements = new ArrayList<>(); try (Reader in = new FileReader(measurementCsvLocation)) { Iterable<CSVRecord> records = CSVFormat.DEFAULT.withDelimiter(';').parse(in); for (CSVRecord record : records) { Long timeStamp = Long.parseLong(record.get(0)); Double temperature = Double.parseDouble(record.get(1)); measurements.add(new Measurement(timeStamp, temperature)); } } catch (IOException e) { e.printStackTrace(); } return measurements; }
@Test 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); Foo foo = mapper.mapRecord(record); assertThat(foo).isNotNull(); assertThat(foo.getFirstName()).isEqualTo("foo"); assertThat(foo.getLastName()).isEqualTo("bar"); assertThat(foo.getAge()).isEqualTo(15); assertThat(foo.isMarried()).isTrue(); }
@Test(expected = IllegalArgumentException.class) public void testDelimiterSameAsEscapeThrowsException() { CSVFormat.DEFAULT.withDelimiter('!').withEscape('!'); }
@Test(expected = IllegalArgumentException.class) public void testDelimiterSameAsCommentStartThrowsException() { CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!'); }
@Test(expected = IllegalArgumentException.class) public void testWithDelimiterLFThrowsException() { CSVFormat.DEFAULT.withDelimiter(LF); }
@Test public void testWithDelimiter() throws Exception { final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!'); assertEquals('!', formatWithDelimiter.getDelimiter()); }
protected CSVFormat getFormat() { return CSVFormat.DEFAULT.withDelimiter(';').withRecordSeparator('\n'); }