<dependency> <groupId>net.sf.supercsv</groupId> <artifactId>super-csv</artifactId> <version>VERSION</version> </dependency>Replace VERSION by the actual version number, i.e. 2.1.0 is the current version of Super CSV.
isbn,title,author,publisher,published,price 0321356683,Effective Java,Joshua Bloch,Addision-Wesley,05/08/2008,38 0321356683,Head First Java,Kathy Sierra & Bert Bates,O'Reilly Media,02/09/2005,30 0131872486,Thinking in Java,Bruce Eckel,Prentice Hall,02/26/2006,45 0596527756,Java Generics and Collections,Naftalin & Philip Wadler,O'Reilly Media,10/24/2006,27Note that the first line contains the header columns, and the subsequent lines are actual data. The column delimiter here is comma.
package net.codejava.supercsv; import java.util.Date; public class Book { private String isbn; private String title; private String author; private String publisher; private Date published; private double price; public Book() { // this empty constructor is required } public Book(String isbn, String title, String author, String publisher, Date published, double price) { this.isbn = isbn; this.title = title; this.author = author; this.publisher = publisher; this.published = published; this.price = price; } // getters and setters }This POJO class defines fields that match the column headers in the CSV file. Remember to supply complete code for the getters and setters.
CellProcessor[] processors = new CellProcessor[] { new NotNull(), // ISBN new NotNull(), // title new NotNull(), // author new NotNull(), // publisher new ParseDate("MM/dd/yyyy"), // published date new ParseDouble() // price };Note that the order of processors must match the order of columns in the CSV file.
ICsvBeanReader beanReader = new CsvBeanReader(new FileReader(csvFileName), CsvPreference.STANDARD_PREFERENCE); String[] header = beanReader.getHeader(true); Book bookBean = null; while ((bookBean = beanReader.read(Book.class, header, processors)) != null) { // deals with each bean read String ISBN = bookBean.getIsbn(); //... }Here, the while loop reads each line (after the first line of column headers) into a Book object until reaching the end of the CSV file. Which column is mapped to which Book’s field is specified by the header array:
String[] header = beanReader.getHeader(true);In this case, the header is read from the first line in the CSV file, and the column names must match the field names in the JavaBean class:
isbn,title,author,publisher,published,priceThe following code is a complete method:
static void readCSVFile(String csvFileName) { ICsvBeanReader beanReader = null; CellProcessor[] processors = new CellProcessor[] { new NotNull(), // ISBN new NotNull(), // title new NotNull(), // author new NotNull(), // publisher new ParseDate("MM/dd/yyyy"), // published date new ParseDouble() // price }; try { beanReader = new CsvBeanReader(new FileReader(csvFileName), CsvPreference.STANDARD_PREFERENCE); String[] header = beanReader.getHeader(true); Book bookBean = null; while ((bookBean = beanReader.read(Book.class, header, processors)) != null) { System.out.printf("%s %-30s %-30s %-20s %tD $%.2f", bookBean.getIsbn(), bookBean.getTitle(), bookBean.getAuthor(), bookBean.getPublisher(), bookBean.getPublished(), bookBean.getPrice()); System.out.println(); } } catch (FileNotFoundException ex) { System.err.println("Could not find the CSV file: " + ex); } catch (IOException ex) { System.err.println("Error reading the CSV file: " + ex); } finally { if (beanReader != null) { try { beanReader.close(); } catch (IOException ex) { System.err.println("Error closing the reader: " + ex); } } } }This method reads data out from the specified CSV file, with each line is read into a Book object, and then prints the book details to the standard output.
package net.codejava.supercsv; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import org.supercsv.cellprocessor.ParseDate; import org.supercsv.cellprocessor.ParseDouble; import org.supercsv.cellprocessor.constraint.NotNull; import org.supercsv.cellprocessor.ift.CellProcessor; import org.supercsv.io.CsvBeanReader; import org.supercsv.io.ICsvBeanReader; import org.supercsv.prefs.CsvPreference; /** * This program demonstrates how to read CSV file using SuperCSV library. * Each line is read into a JavaBean class (POJO). * @author www.codejava.net * */ public class CsvBeanReaderExample { static void readCSVFile(String csvFileName) { // same code as the method above } public static void main(String[] args) { String csvFileName = "Books.csv"; readCSVFile(csvFileName); } }Output:
0321356683 Effective Java Joshua Bloch Addision-Wesley 05/08/08 $38.00 0321356683 Head First Java Kathy Sierra & Bert Bates O'Reilly Media 02/09/05 $30.00 0131872486 Thinking in Java Bruce Eckel Prentice Hall 02/26/06 $45.00 0596527756 Java Generics and Collections Naftalin & Philip Wadler O'Reilly Media 10/24/06 $27.00
You can get the code example on GitHub or download the sample code below.
To write data to a CSV file, refer to this tutorial: Java Write CSV File Example