Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6e29dcf

Browse files
committed
Java Example Code for Reading CSV File with Super CSV
0 parents  commit 6e29dcf

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

Books.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
isbn,title,author,publisher,published,price
2+
0321356683,Effective Java,Joshua Bloch,Addision-Wesley,05/08/2008,38
3+
0321356683,Head First Java,Kathy Sierra & Bert Bates,O'Reilly Media,02/09/2005,30
4+
0131872486,Thinking in Java,Bruce Eckel,Prentice Hall,02/26/2006,45
5+
0596527756,Java Generics and Collections,Naftalin & Philip Wadler,O'Reilly Media,10/24/2006,27

src/net/code/java/supercsv/Book.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package net.codejava.supercsv;
2+
3+
import java.util.Date;
4+
5+
public class Book {
6+
private String isbn;
7+
private String title;
8+
private String author;
9+
private String publisher;
10+
private Date published;
11+
private double price;
12+
13+
public Book() {
14+
// this empty constructor is required
15+
}
16+
17+
public Book(String isbn, String title, String author, String publisher,
18+
Date published, double price) {
19+
this.isbn = isbn;
20+
this.title = title;
21+
this.author = author;
22+
this.publisher = publisher;
23+
this.published = published;
24+
this.price = price;
25+
}
26+
27+
public String getIsbn() {
28+
return isbn;
29+
}
30+
31+
public void setIsbn(String isbn) {
32+
this.isbn = isbn;
33+
}
34+
35+
public String getTitle() {
36+
return title;
37+
}
38+
39+
public void setTitle(String title) {
40+
this.title = title;
41+
}
42+
43+
public String getAuthor() {
44+
return author;
45+
}
46+
47+
public void setAuthor(String author) {
48+
this.author = author;
49+
}
50+
51+
public String getPublisher() {
52+
return publisher;
53+
}
54+
55+
public void setPublisher(String publisher) {
56+
this.publisher = publisher;
57+
}
58+
59+
public Date getPublished() {
60+
return published;
61+
}
62+
63+
public void setPublished(Date published) {
64+
this.published = published;
65+
}
66+
67+
public double getPrice() {
68+
return price;
69+
}
70+
71+
public void setPrice(double price) {
72+
this.price = price;
73+
}
74+
75+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.codejava.supercsv;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
import org.supercsv.cellprocessor.ParseDate;
8+
import org.supercsv.cellprocessor.ParseDouble;
9+
import org.supercsv.cellprocessor.constraint.NotNull;
10+
import org.supercsv.cellprocessor.ift.CellProcessor;
11+
import org.supercsv.io.CsvBeanReader;
12+
import org.supercsv.io.ICsvBeanReader;
13+
import org.supercsv.prefs.CsvPreference;
14+
15+
/**
16+
* This program demonstrates how to read CSV file using SuperCSV library.
17+
* Each line is read into a JavaBean class (POJO).
18+
* @author www.codejava.net
19+
*
20+
*/
21+
public class CsvBeanReaderExample {
22+
23+
static void readCSVFile(String csvFileName) {
24+
ICsvBeanReader beanReader = null;
25+
CellProcessor[] processors = new CellProcessor[] {
26+
new NotNull(), // ISBN
27+
new NotNull(), // title
28+
new NotNull(), // author
29+
new NotNull(), // publisher
30+
new ParseDate("MM/dd/yyyy"), // published date
31+
new ParseDouble() // price
32+
};
33+
34+
try {
35+
beanReader = new CsvBeanReader(new FileReader(csvFileName),
36+
CsvPreference.STANDARD_PREFERENCE);
37+
String[] header = beanReader.getHeader(true);
38+
Book bookBean = null;
39+
while ((bookBean = beanReader.read(Book.class, header, processors)) != null) {
40+
System.out.printf("%s %-30s %-30s %-20s %tD $%.2f",
41+
bookBean.getIsbn(), bookBean.getTitle(),
42+
bookBean.getAuthor(), bookBean.getPublisher(),
43+
bookBean.getPublished(), bookBean.getPrice());
44+
System.out.println();
45+
}
46+
} catch (FileNotFoundException ex) {
47+
System.err.println("Could not find the CSV file: " + ex);
48+
} catch (IOException ex) {
49+
System.err.println("Error reading the CSV file: " + ex);
50+
} finally {
51+
if (beanReader != null) {
52+
try {
53+
beanReader.close();
54+
} catch (IOException ex) {
55+
System.err.println("Error closing the reader: " + ex);
56+
}
57+
}
58+
}
59+
}
60+
61+
public static void main(String[] args) {
62+
String csvFileName = "Books.csv";
63+
readCSVFile(csvFileName);
64+
}
65+
}

0 commit comments

Comments
 (0)