This document only describes the breaking changes when migrating from FastCSV 1.x to 3.x.
For a full list of changes, including new features, see the changelog .
Reader
Configuring the reader
Old way:
CsvReader csvReader = new CsvReader () ;
csvReader . setFieldSeparator ( ' , ' ) ;
csvReader . setTextDelimiter ( ' " ' ) ;
csvReader . setSkipEmptyRows ( true ) ;
csvReader . setErrorOnDifferentFieldCount ( false ) ;
New way:
. ignoreDifferentFieldCount ( true ) ;
Reading data from file
Old way:
try ( CsvParser csvParser = new CsvReader () . parse ( file, UTF_8 ) ) {
while ((row = csvParser . nextRow () ) != null ) {
System . out . println ( " First field of row: " + row . getField ( 0 )) ;
New way:
try ( CsvReader < CsvRecord > csv = CsvReader . builder () . ofCsvRecord ( file ) ) {
System . out . println ( " First field of record: " + rec . getField ( 0 ))
Old way:
CsvReader csvReader = new CsvReader () ;
csvReader . setContainsHeader ( true ) ;
try ( CsvParser csvParser = csvReader . parse ( file, UTF_8 ) ) {
while ((row = csvParser . nextRow () ) != null ) {
System . out . println ( " Field named firstname: " + row . getField ( " firstname " )) ;
New way:
try ( CsvReader < NamedCsvRecord > csv = CsvReader . builder () . ofNamedCsvRecord ( file ) ) {
System . out . println ( " Field named firstname: " + rec . getField ( " firstname " ))
Read an entire file at once
Old way:
CsvContainer csv = new CsvReader () . read ( file, UTF_8 ) ;
New way:
The container concept has been removed, but you can
easily build something similar using the Java Stream API.
try ( CsvReader < CsvRecord > csvReader = CsvReader . builder () . ofCsvRecord ( file ) ) {
data = csvReader . stream () . toList () ;
Writer
Configuring the writer
Old way:
CsvWriter csvWriter = new CsvWriter () ;
csvWriter . setFieldSeparator ( ' , ' ) ;
csvWriter . setTextDelimiter ( ' " ' ) ;
csvWriter . setAlwaysDelimitText ( true ) ;
csvWriter . setLineDelimiter ( new char []{ ' \r ' , ' \n ' } ) ;
New way:
. quoteStrategy ( QuoteStrategies . ALWAYS )
. lineDelimiter ( LineDelimiter . CRLF ) ;
Write to file
Old way:
try ( CsvAppender csvAppender = new CsvWriter () . append ( file ) ) {
csvAppender . appendLine ( " header1 " , " header2 " ) ;
csvAppender . appendLine ( " value1 " , " value2 " ) ;
New way:
try ( CsvWriter csvWriter = CsvWriter . builder () . build ( file ) ) {
. writeRecord ( " header1 " , " header2 " )
. writeRecord ( " value1 " , " value2 " ) ;
Write to writer
Old way:
Writer writer = new StringWriter () ;
try ( CsvAppender csvAppender = new CsvWriter () . append ( writer ) ) {
csvAppender . appendLine ( " header1 " , " header2 " ) ;
csvAppender . appendLine ( " value1 " , " value2 " ) ;
New way:
Writer writer = new StringWriter () ;
try ( CsvWriter csvWriter = CsvWriter . builder () . build ( writer ) ) {
. writeRecord ( " header1 " , " header2 " )
. writeRecord ( " value1 " , " value2 " ) ;
Caution
Be aware of a change in the semantic in FastCSV.
In version 3.x you probably want to pass in a java.io.BufferedWriter
for proper
performance. The opposite was recommended in version 1.x.
Check the Javadoc for further information.