public static void main(String[] args) throws FileNotFoundException, IOException { CitiBikeReader<Trip> reader = new CitiBikeReader<>("/tmp", str -> new TripParser(str)); EntityInfoBuilder<Trip> entityInfo = new EntityInfoBuilder<>(Trip.class); entityInfo.addFields("bikeId", "starttime", "startStationId", "stoptime", "endStationId", "userType"); CsvConfiguration<Trip> csvConfiguration = new Builder<>(Trip.class) .entityInfo(entityInfo.build()) .alwaysDelimitText(true) .fieldSeparator(';') .lineDelimiter("\n") .textDelimiter('\'') .header(false) .build(); CsvWriter<Trip> csvWriter = new CsvWriter<>(csvConfiguration); try (FileOutputStream fos = new FileOutputStream(new File("/tmp/trips.csv"))) { reader.forEachCsvInZip(trips -> { try { csvWriter.writeAll(fos, trips); } catch (IOException e) { e.printStackTrace(); } }); } }
@Test public void escapeLineDelimiterTest() throws IOException { config.textDelimiter('\''); String result = writeCsvToString(config.build(), new SomeEntity("John \nSmith", 10)); assertEquals("name,age" + ls + "'John \nSmith',10" + ls, result); }
@Test public void optionalTextDelimiterUsedTest() throws IOException { config.textDelimiter('"'); String result = writeCsvToString(config.build(), new SomeEntity("John \"Smith\"", 10)); assertEquals("name,age" + ls + "\"John \"\"Smith\"\"\",10" + ls, result); }
@Test public void optionalTextDelimiterNotUsedTest() throws IOException { config.textDelimiter('$'); String result = writeCsvToString(config.build(), new SomeEntity("John", 10)); assertEquals("name,age" + ls + "John,10" + ls, result); }
@Test public void mandatoryTextDelimiterUsedTest() throws IOException { config.textDelimiter('\'').alwaysDelimitText(true); String result = writeCsvToString(config.build(), new SomeEntity("John 'Smith'", 10), new SomeEntity("Amanda", 20)); assertEquals("'name','age'" + ls + "'John ''Smith''','10'" + ls + "'Amanda','20'" + ls, result); }
@Test public void escapeFieldSeparatorTest() throws IOException { config.fieldSeparator(';').textDelimiter('\''); String result = writeCsvToString(config.build(), new SomeEntity("John;Smith", 10)); assertEquals("name;age" + ls + "'John;Smith';10" + ls, result); }