Csv { public static Iterable<Iterable<String>> asIterable(final Reader preader) { return CSV.asIterable(preader); } private  Csv(); static String[] readSystemProperty(final String propertyName, final String... defaults); static void writeCsvRow(final Appendable writer, final Object... elems); static void writeCsvRow2(final Appendable writer, final Object obj, final Object... elems); static void writeCsvRow(final Appendable writer, final long... elems); static void writeCsvRowNoEOL(final long[] elems, final Appendable writer); static void writeCsvRow(final Appendable writer, final Iterable<?> elems); static void writeCsvRowNoEOL(final Iterable<?> elems, final Appendable writer); static T read(final File file, final Charset charset,
          final CsvMapHandler<T> handler); static T read(final File file, final Charset charset,
          final CsvHandler<T> handler); static List<Map<String, String>> read(final Reader preader); static T read(final Reader preader,
          final CsvMapHandler<T> handler); static List<String> readRow(final String row); static List<String> readRow(final Reader reader); static T readRow(final Reader reader, final CsvRowHandler<T> handler); static T read(final Reader preader,
          final CsvHandler<T> handler); static T readNoBom(final PushbackReader reader, final CsvHandler<T> handler); static Iterable<Iterable<String>> asIterable(final Reader preader); static CsvReader reader(final Reader preader); static CsvReader readerNoBOM(final PushbackReader reader); static void writeCsvElement(final CharSequence elem, final Appendable writer); static void writeQuotedCsvElement(final CharSequence elem, final Appendable writer); static CharSequence toCsvElement(final CharSequence elem); @CheckReturnValue static int readCsvElement(final Reader reader, final StringBuilder addElemTo); static final CharSeparatedValues CSV; }
@Test public void testLineIteration() { int nr = 0; for (Iterable<String> line : Csv.asIterable(new StringReader("bla,\"bla\"\nuhu,uhu2\n"))) { LOG.debug("{}", line); nr++; } Assert.assertEquals(2, nr); }
@Test(expected = UncheckedCsvParseException.class) public void testLineIterationError() { for (Iterable<String> line : Csv.asIterable(new StringReader("bla,\"bla"))) { LOG.debug("{}", line); } Assert.fail(); }
@Test public void testCsvFileParsingReader() throws IOException, CsvParseException { try (InputStream resourceAsStream = CsvTest.class.getResourceAsStream("/test.csv")) { Iterable<Iterable<String>> asIterable = Csv.asIterable(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8)); int i = 0; for (Iterable<String> row : asIterable) { LOG.debug("Row {}", row); i++; } Assert.assertEquals(4, i); } }
@Test public void testCsvFileParsing2() throws IOException { try (InputStream resourceAsStream = CsvTest.class.getResourceAsStream("/test.csv")) { Iterable<Iterable<String>> asIterable = Csv.asIterable(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8)); int i = 0; List<String> lastRow = null; for (Iterable<String> row : asIterable) { lastRow = Lists.newArrayList(row); LOG.debug("row {}", row); i++; } Assert.assertEquals(4, i); Assert.assertEquals("3,,\nasdg,,ahsd\nsdf", lastRow.get(0)); } }