@Test public void testRead() { CSVRead.read(CSVReadTest.class.getResourceAsStream("/a.csv"), new CSVReadDeal<CsvReadDto>() { @Override public CsvReadDto dealBean(String[] arr) { CsvReadDto dto = new CsvReadDto(); dto.setId(Long.valueOf(arr[0])); dto.setName(arr[1]); dto.setAge(Integer.valueOf(arr[2])); return dto; } @Override public void dealBatchBean(List<CsvReadDto> list) { Assert.assertEquals("name1", list.get(0).getName()); Assert.assertEquals("name2", list.get(1).getName()); Assert.assertEquals("name3", list.get(2).getName()); } }); }
public static <E> void read(InputStream inputStream, CSVReadDeal<E> deal) { Objects.requireNonNull(inputStream); Objects.requireNonNull(deal); try (CSVReader reader = new CSVReader(new InputStreamReader(new DataInputStream(inputStream)))) { int tmp = deal.getBatchCount(); List<E> l = new ArrayList<>(tmp); int i = 0; String [] arr; while ((arr = reader.readNext()) != null) { ++i; if (i <= deal.skipLine()) { continue; } E o = deal.dealBean(arr); if (o != null) { l.add(o); if (i % tmp == 0) { deal.dealBatchBean(l); l = new ArrayList<>(tmp); } } } if (!l.isEmpty()) { deal.dealBatchBean(l); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { inputStream = null; } } }
CSVRead { public static <E> void read(InputStream inputStream, CSVReadDeal<E> deal) { Objects.requireNonNull(inputStream); Objects.requireNonNull(deal); try (CSVReader reader = new CSVReader(new InputStreamReader(new DataInputStream(inputStream)))) { int tmp = deal.getBatchCount(); List<E> l = new ArrayList<>(tmp); int i = 0; String [] arr; while ((arr = reader.readNext()) != null) { ++i; if (i <= deal.skipLine()) { continue; } E o = deal.dealBean(arr); if (o != null) { l.add(o); if (i % tmp == 0) { deal.dealBatchBean(l); l = new ArrayList<>(tmp); } } } if (!l.isEmpty()) { deal.dealBatchBean(l); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { inputStream = null; } } } }
CSVRead { public static <E> void read(InputStream inputStream, CSVReadDeal<E> deal) { Objects.requireNonNull(inputStream); Objects.requireNonNull(deal); try (CSVReader reader = new CSVReader(new InputStreamReader(new DataInputStream(inputStream)))) { int tmp = deal.getBatchCount(); List<E> l = new ArrayList<>(tmp); int i = 0; String [] arr; while ((arr = reader.readNext()) != null) { ++i; if (i <= deal.skipLine()) { continue; } E o = deal.dealBean(arr); if (o != null) { l.add(o); if (i % tmp == 0) { deal.dealBatchBean(l); l = new ArrayList<>(tmp); } } } if (!l.isEmpty()) { deal.dealBatchBean(l); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { inputStream = null; } } }  }
CSVRead { public static <E> void read(InputStream inputStream, CSVReadDeal<E> deal) { Objects.requireNonNull(inputStream); Objects.requireNonNull(deal); try (CSVReader reader = new CSVReader(new InputStreamReader(new DataInputStream(inputStream)))) { int tmp = deal.getBatchCount(); List<E> l = new ArrayList<>(tmp); int i = 0; String [] arr; while ((arr = reader.readNext()) != null) { ++i; if (i <= deal.skipLine()) { continue; } E o = deal.dealBean(arr); if (o != null) { l.add(o); if (i % tmp == 0) { deal.dealBatchBean(l); l = new ArrayList<>(tmp); } } } if (!l.isEmpty()) { deal.dealBatchBean(l); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { inputStream = null; } } }  static void read(InputStream inputStream, CSVReadDeal<E> deal); static void read(InputStream inputStream, CSVReadDeal<E> deal, CSVParser parser); static void read(InputStream inputStream, CSVReadDeal<E> deal, Function<Reader, CSVReader> function); }
CSVRead { public static <E> void read(InputStream inputStream, CSVReadDeal<E> deal) { Objects.requireNonNull(inputStream); Objects.requireNonNull(deal); try (CSVReader reader = new CSVReader(new InputStreamReader(new DataInputStream(inputStream)))) { int tmp = deal.getBatchCount(); List<E> l = new ArrayList<>(tmp); int i = 0; String [] arr; while ((arr = reader.readNext()) != null) { ++i; if (i <= deal.skipLine()) { continue; } E o = deal.dealBean(arr); if (o != null) { l.add(o); if (i % tmp == 0) { deal.dealBatchBean(l); l = new ArrayList<>(tmp); } } } if (!l.isEmpty()) { deal.dealBatchBean(l); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { inputStream = null; } } }  static void read(InputStream inputStream, CSVReadDeal<E> deal); static void read(InputStream inputStream, CSVReadDeal<E> deal, CSVParser parser); static void read(InputStream inputStream, CSVReadDeal<E> deal, Function<Reader, CSVReader> function);  }