Csv { public static <T> T read(final File file, final Charset charset, final CsvMapHandler<T> handler) throws IOException, CsvParseException { return CSV.read(file, charset, handler); } 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 testCsvReadWrite() throws IOException, CsvParseException { File testFile = createTestCsv(); List<Map<String, String>> data = Csv.read(testFile, StandardCharsets.UTF_8, new Csv.CsvHandler<List<Map<String, String>>>() { private boolean firstRow = true; private final List<Map<String, String>> result = new ArrayList<>(); private List<String> header = new ArrayList<>(); private Map<String, String> row = null; private int i = 0; @Override public void startRow() { if (!firstRow) { row = Maps.newHashMapWithExpectedSize(header.size()); i = 0; } } @Override public void element(final CharSequence elem) { if (firstRow) { header.add(elem.toString()); } else { row.put(header.get(i++), elem.toString()); } } @Override public void endRow() { if (firstRow) { firstRow = false; } else { result.add(row); } } @Override public List<Map<String, String>> eof() { return result; } }); Map<String, String> d0 = data.get(0); Assert.assertEquals("1,3", d0.get("c")); Assert.assertEquals("1", d0.get("d")); Map<String, String> d1 = data.get(1); Assert.assertEquals("1,3", d1.get("d")); Assert.assertEquals("\"", d1.get("b")); Assert.assertEquals("0\n", d1.get("c")); }
@Test(expected = CsvParseException.class) public void testCsvReadWriteException() throws IOException, CsvParseException { File testFile = createTestCsv(); Csv.read(testFile, StandardCharsets.UTF_8, new Csv.CsvHandler<Void>() { @Override public void element(final CharSequence elem) throws CsvParseException { throw new CsvParseException("Yohooo at " + elem); } @Override public Void eof() { return null; } }); }
@Test(expected = IllegalArgumentException.class) public void testCsvReadWriteException2() throws IOException, CsvParseException { File testFile = createTestCsv(); Csv.read(testFile, StandardCharsets.UTF_8, new Csv.CsvHandler<Void>() { @Override public void element(final CharSequence elem) { throw new IllegalArgumentException("Yohooo at " + elem); } @Override public Void eof() { return null; } }); }
@Test public void testCsvReadWrite2() throws IOException, CsvParseException { File testFile = createTestCsv(); List<Map<String, String>> data = Csv.read(testFile, StandardCharsets.UTF_8, new Csv.CsvMapHandler<List<Map<String, String>>>() { private final List<Map<String, String>> result = new ArrayList<>(); @Override public void row(final Map<String, String> row) { result.add(row); } @Override public List<Map<String, String>> eof() { return result; } }); Map<String, String> d0 = data.get(0); Assert.assertEquals("1,3", d0.get("c")); Assert.assertEquals("1", d0.get("d")); Map<String, String> d1 = data.get(1); Assert.assertEquals("1,3", d1.get("d")); Assert.assertEquals("\"", d1.get("b")); Assert.assertEquals("0\n", d1.get("c")); }
@Test @Ignore public void testLargeFileRead() throws IOException, CsvParseException { try (BufferedReader reader = new BufferedReader(new InputStreamReader( new GZIPInputStream( new URL("http: StandardCharsets.UTF_8), 65536)) { long startTime = System.currentTimeMillis(); long count = Csv.read(reader, new Csv.CsvHandler<Long>() { private long count = 0; @Override public void element(final CharSequence elem) { } @Override public void endRow() { count++; } @Override public Long eof() { return count; } }); LOG.debug("Line count is {} in {}", count, (System.currentTimeMillis() - startTime)); Assert.assertEquals(3173959L, count); } }
@Test public void testCsvFileParsing() throws IOException, CsvParseException { try (InputStream resourceAsStream = CsvTest.class.getResourceAsStream("/test.csv")) { int nr = Csv.read(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8), new Csv.CsvMapHandler<Integer>() { private int i = 0; @Override public void row(final Map<String, String> row) { LOG.debug("Row {}", row); i++; } @Override public Integer eof() { return i; } }); Assert.assertEquals(3, nr); } }
@Test(expected = CsvParseException.class) public void testCsvFileParsingBad() throws IOException, CsvParseException { try (InputStream resourceAsStream = CsvTest.class.getResourceAsStream("/test_bad.csv")) { Csv.read(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8), new Csv.CsvMapHandler<Void>() { @Override public void row(final Map<String, String> row) { LOG.debug("Row {}", row); } @Override public Void eof() { return null; } }); } }
@Test public void testCsvReadNrRows() throws IOException, CsvParseException { Integer read = Csv.read(new StringReader("a,b,c\nc,d,e\n"), new AssertCsv()); Assert.assertEquals(2, read.intValue()); read = Csv.read(new StringReader("a,b,c\r\nc,d,e\n\r"), new AssertCsv()); Assert.assertEquals(2, read.intValue()); read = Csv.read(new StringReader("a,b,c\r\nc,d,e"), new AssertCsv()); Assert.assertEquals(2, read.intValue()); }