參考:
csv文件處理——Opencsv - 簡書 (jianshu.com)
csv導入導出(opencsv)_鄭重其事,鵬程萬里的博客-CSDN博客_opencsv
3. CSV新版本
上述的在Opencsv4.0版本以上已經廢棄了。采用CSVReaderBuilder來代替。本質上是采用的建造者模式來構建對象,更加優雅。
3.1 構建CSVReader對象
可以使用readNext或者readAll進行逐行解讀。
try { InputStreamReader is = new InputStreamReader(new FileInputStream(fileName), "utf-8"); CSVParser csvParser = new CSVParserBuilder().withSeparator('\t').build(); CSVReader reader = new CSVReaderBuilder(is).withCSVParser(csvParser).build(); List<String[]> strings = reader.readAll(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
3.2 CsvToBeanBuilder
優雅的解析文檔中的字段。將CSV文件轉換為Bean對象。
此外,opencsv提供了基於"策略"的映射,將CSV綁定到bean。