├── .gitignore
├── Readme.md
├── java-csv-file-handling-with-apache-commons-csv
├── java-csv-file-handling-with-apache-commons-csv.iml
├── pom.xml
├── sample.csv
├── src
│ └── main
│ │ └── java
│ │ ├── BasicCSVReader.java
│ │ ├── CSVReaderWithHeaderAutoDetection.java
│ │ ├── CSVReaderWithManualHeader.java
│ │ └── CSVWriter.java
├── users-with-header.csv
└── users.csv
└── java-csv-file-handling-with-opencsv
├── java-csv-file-handling-with-opencsv.iml
├── object-list-sample.csv
├── pom.xml
├── src
└── main
│ └── java
│ ├── CSVUser.java
│ ├── MyUser.java
│ ├── OpenCSVParseToBeanWithoutAnnotation.java
│ ├── OpenCSVReadAndParseToBean.java
│ ├── OpenCSVReader.java
│ └── OpenCSVWriter.java
├── string-array-sample.csv
├── users-with-header.csv
└── users.csv
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | target
3 | .idea
4 | .DS_Store
5 | *.iml
6 |
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | Learn how to read and write CSV files in Java.
2 |
3 | ## Relevant Tutorials
4 |
5 | 1. [How to Read and Write CSV files in Java using Apache Commons CSV](https://www.callicoder.com/java-read-write-csv-file-apache-commons-csv/)
6 |
7 | 2. [How to Read and Write CSV files in Java using OpenCSV](https://www.callicoder.com/java-read-write-csv-file-opencsv/)
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/java-csv-file-handling-with-apache-commons-csv.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.callicoder
8 | csv-handling
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | org.apache.commons
14 | commons-csv
15 | 1.5
16 |
17 |
18 |
19 |
20 |
21 | org.apache.maven.plugins
22 | maven-compiler-plugin
23 |
24 | 1.8
25 | 1.8
26 |
27 |
28 |
29 | org.apache.maven.plugins
30 | maven-dependency-plugin
31 | 3.0.1
32 |
33 |
34 | copy-dependencies
35 | package
36 |
37 | copy-dependencies
38 |
39 |
40 |
41 | ${project.build.directory}/lib/
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.apache.maven.plugins
49 | maven-jar-plugin
50 | 3.0.2
51 |
52 |
53 |
54 | true
55 | lib/
56 | BasicCSVReader
57 |
58 |
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/sample.csv:
--------------------------------------------------------------------------------
1 | ID,Name,Designation,Company
2 | 1,Sundar Pichai ♥,CEO,Google
3 | 2,Satya Nadella,CEO,Microsoft
4 | 3,Tim cook,CEO,Apple
5 | 4,Mark Zuckerberg,CEO,Facebook
6 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/src/main/java/BasicCSVReader.java:
--------------------------------------------------------------------------------
1 | import org.apache.commons.csv.CSVFormat;
2 | import org.apache.commons.csv.CSVParser;
3 | import org.apache.commons.csv.CSVRecord;
4 |
5 | import java.io.IOException;
6 | import java.io.Reader;
7 | import java.nio.file.Files;
8 | import java.nio.file.Paths;
9 |
10 | /**
11 | * Created by rajeevkumarsingh on 25/09/17.
12 | */
13 | public class BasicCSVReader {
14 | private static final String SAMPLE_CSV_FILE_PATH = "./users.csv";
15 |
16 | public static void main(String[] args) throws IOException {
17 | try (
18 | Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
19 | CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
20 | ) {
21 | for (CSVRecord csvRecord : csvParser) {
22 | // Accessing Values by Column Index
23 |
24 | String name = csvRecord.get(0);
25 | String email = csvRecord.get(1);
26 | String phone = csvRecord.get(2);
27 | String country = csvRecord.get(3);
28 |
29 | System.out.println("Record No - " + csvRecord.getRecordNumber());
30 | System.out.println("---------------");
31 | System.out.println("Name : " + name);
32 | System.out.println("Email : " + email);
33 | System.out.println("Phone : " + phone);
34 | System.out.println("Country : " + country);
35 | System.out.println("---------------\n\n");
36 | }
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/src/main/java/CSVReaderWithHeaderAutoDetection.java:
--------------------------------------------------------------------------------
1 | import org.apache.commons.csv.CSVFormat;
2 | import org.apache.commons.csv.CSVParser;
3 | import org.apache.commons.csv.CSVRecord;
4 |
5 | import java.io.FileReader;
6 | import java.io.IOException;
7 | import java.io.Reader;
8 | import java.nio.file.Files;
9 | import java.nio.file.Paths;
10 |
11 | /**
12 | * Created by rajeevkumarsingh on 25/09/17.
13 | */
14 | public class CSVReaderWithHeaderAutoDetection {
15 | private static final String SAMPLE_CSV_FILE_PATH = "./users-with-header.csv";
16 |
17 | public static void main(String[] args) throws IOException {
18 | try (
19 | Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
20 | CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT
21 | .withFirstRecordAsHeader()
22 | .withIgnoreHeaderCase()
23 | .withTrim());
24 | ) {
25 | for (CSVRecord csvRecord : csvParser) {
26 | // Accessing values by Header names
27 |
28 | String name = csvRecord.get("Name");
29 | String email = csvRecord.get("Email");
30 | String phone = csvRecord.get("Phone");
31 | String country = csvRecord.get("Country");
32 |
33 | System.out.println("Record No - " + csvRecord.getRecordNumber());
34 | System.out.println("---------------");
35 | System.out.println("Name : " + name);
36 | System.out.println("Email : " + email);
37 | System.out.println("Phone : " + phone);
38 | System.out.println("Country : " + country);
39 | System.out.println("---------------\n\n");
40 | }
41 | }
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/src/main/java/CSVReaderWithManualHeader.java:
--------------------------------------------------------------------------------
1 | import org.apache.commons.csv.CSVFormat;
2 | import org.apache.commons.csv.CSVParser;
3 | import org.apache.commons.csv.CSVRecord;
4 |
5 | import java.io.FileReader;
6 | import java.io.IOException;
7 | import java.io.Reader;
8 | import java.nio.file.Files;
9 | import java.nio.file.Paths;
10 |
11 | /**
12 | * Created by rajeevkumarsingh on 25/09/17.
13 | */
14 | public class CSVReaderWithManualHeader {
15 | private static final String SAMPLE_CSV_FILE_PATH = "./users.csv";
16 |
17 | public static void main(String[] args) throws IOException {
18 | try (
19 | Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
20 | CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT
21 | .withHeader("Name", "Email", "Phone", "Country")
22 | .withIgnoreHeaderCase()
23 | .withTrim());
24 | ) {
25 |
26 | for (CSVRecord csvRecord : csvParser) {
27 | // Accessing values by the names assigned to each column
28 |
29 | String name = csvRecord.get("Name");
30 | String email = csvRecord.get("Email");
31 | String phone = csvRecord.get("Phone");
32 | String country = csvRecord.get("Country");
33 |
34 | System.out.println("Record No - " + csvRecord.getRecordNumber());
35 | System.out.println("---------------");
36 | System.out.println("Name : " + name);
37 | System.out.println("Email : " + email);
38 | System.out.println("Phone : " + phone);
39 | System.out.println("Country : " + country);
40 | System.out.println("---------------\n\n");
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/src/main/java/CSVWriter.java:
--------------------------------------------------------------------------------
1 | import org.apache.commons.csv.CSVFormat;
2 | import org.apache.commons.csv.CSVPrinter;
3 |
4 | import java.io.*;
5 | import java.nio.file.Files;
6 | import java.nio.file.Paths;
7 | import java.util.Arrays;
8 |
9 | /**
10 | * Created by rajeevkumarsingh on 25/09/17.
11 | */
12 |
13 | public class CSVWriter {
14 | private static final String SAMPLE_CSV_FILE = "./sample.csv";
15 |
16 | public static void main(String[] args) throws IOException {
17 |
18 | try (
19 | BufferedWriter writer = Files.newBufferedWriter(Paths.get(SAMPLE_CSV_FILE));
20 |
21 | CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
22 | .withHeader("ID", "Name", "Designation", "Company"));
23 | ) {
24 | csvPrinter.printRecord("1", "Sundar Pichai ♥", "CEO", "Google");
25 | csvPrinter.printRecord("2", "Satya Nadella", "CEO", "Microsoft");
26 | csvPrinter.printRecord("3", "Tim cook", "CEO", "Apple");
27 |
28 | csvPrinter.printRecord(Arrays.asList("4", "Mark Zuckerberg", "CEO", "Facebook"));
29 |
30 | csvPrinter.flush();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/users-with-header.csv:
--------------------------------------------------------------------------------
1 | Name,Email,Phone,Country
2 | Rajeev Kumar Singh ♥,rajeevs@example.com,+91-9999999999,India
3 | Sachin Tendulkar,sachin@example.com,+91-9999999998,India
4 | Barak Obama,barak.obama@example.com,+1-1111111111,United States
5 | Donald Trump,donald.trump@example.com,+1-2222222222,United States
--------------------------------------------------------------------------------
/java-csv-file-handling-with-apache-commons-csv/users.csv:
--------------------------------------------------------------------------------
1 | Rajeev Kumar Singh ♥,rajeevs@example.com,+91-9999999999,India
2 | Sachin Tendulkar,sachin@example.com,+91-9999999998,India
3 | Barak Obama,barak.obama@example.com,+1-1111111111,United States
4 | Donald Trump,donald.trump@example.com,+1-2222222222,United States
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/java-csv-file-handling-with-opencsv.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/object-list-sample.csv:
--------------------------------------------------------------------------------
1 | country,email,name,phoneNo
2 | India,sundar.pichai@gmail.com,Sundar Pichai ♥,+1-1111111111
3 | India,satya.nadella@outlook.com,Satya Nadella,+1-1111111112
4 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.callicoder
8 | csv-handling
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | com.opencsv
14 | opencsv
15 | 4.0
16 |
17 |
18 |
19 |
20 |
21 |
22 | org.apache.maven.plugins
23 | maven-compiler-plugin
24 | 3.7.0
25 |
26 | 1.8
27 | 1.8
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/src/main/java/CSVUser.java:
--------------------------------------------------------------------------------
1 | import com.opencsv.bean.CsvBindByName;
2 |
3 | /**
4 | * Created by rajeevkumarsingh on 25/09/17.
5 | */
6 | public class CSVUser {
7 | @CsvBindByName
8 | private String name;
9 |
10 | @CsvBindByName(column = "email", required = true)
11 | private String email;
12 |
13 | @CsvBindByName(column = "phone")
14 | private String phoneNo;
15 |
16 | @CsvBindByName
17 | private String country;
18 |
19 | public String getName() {
20 | return name;
21 | }
22 |
23 | public void setName(String name) {
24 | this.name = name;
25 | }
26 |
27 | public String getEmail() {
28 | return email;
29 | }
30 |
31 | public void setEmail(String email) {
32 | this.email = email;
33 | }
34 |
35 | public String getPhoneNo() {
36 | return phoneNo;
37 | }
38 |
39 | public void setPhoneNo(String phoneNo) {
40 | this.phoneNo = phoneNo;
41 | }
42 |
43 | public String getCountry() {
44 | return country;
45 | }
46 |
47 | public void setCountry(String country) {
48 | this.country = country;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/src/main/java/MyUser.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by rajeevkumarsingh on 25/09/17.
3 | */
4 | public class MyUser {
5 | private String name;
6 | private String email;
7 | private String phoneNo;
8 | private String country;
9 |
10 | public MyUser() {
11 |
12 | }
13 |
14 | public MyUser(String name, String email, String phoneNo, String country) {
15 | this.name = name;
16 | this.email = email;
17 | this.phoneNo = phoneNo;
18 | this.country = country;
19 | }
20 |
21 | public String getName() {
22 | return name;
23 | }
24 |
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 | public String getEmail() {
30 | return email;
31 | }
32 |
33 | public void setEmail(String email) {
34 | this.email = email;
35 | }
36 |
37 | public String getPhoneNo() {
38 | return phoneNo;
39 | }
40 |
41 | public void setPhoneNo(String phoneNo) {
42 | this.phoneNo = phoneNo;
43 | }
44 |
45 | public String getCountry() {
46 | return country;
47 | }
48 |
49 | public void setCountry(String country) {
50 | this.country = country;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/src/main/java/OpenCSVParseToBeanWithoutAnnotation.java:
--------------------------------------------------------------------------------
1 | import com.opencsv.bean.ColumnPositionMappingStrategy;
2 | import com.opencsv.bean.CsvToBean;
3 | import com.opencsv.bean.CsvToBeanBuilder;
4 |
5 | import java.io.IOException;
6 | import java.io.Reader;
7 | import java.nio.file.Files;
8 | import java.nio.file.Paths;
9 | import java.util.Iterator;
10 | import java.util.List;
11 |
12 | /**
13 | * Created by rajeevkumarsingh on 25/09/17.
14 | */
15 | public class OpenCSVParseToBeanWithoutAnnotation {
16 | private static final String SAMPLE_CSV_FILE_PATH = "./users-with-header.csv";
17 |
18 | public static void main(String[] args) throws IOException {
19 |
20 | try (
21 | Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
22 | ) {
23 | ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
24 | strategy.setType(MyUser.class);
25 | String[] memberFieldsToBindTo = {"name", "email", "phoneNo", "country"};
26 | strategy.setColumnMapping(memberFieldsToBindTo);
27 |
28 | CsvToBean csvToBean = new CsvToBeanBuilder(reader)
29 | .withMappingStrategy(strategy)
30 | .withSkipLines(1)
31 | .withIgnoreLeadingWhiteSpace(true)
32 | .build();
33 |
34 | Iterator myUserIterator = csvToBean.iterator();
35 |
36 | while (myUserIterator.hasNext()) {
37 | MyUser myUser = myUserIterator.next();
38 | System.out.println("Name : " + myUser.getName());
39 | System.out.println("Email : " + myUser.getEmail());
40 | System.out.println("PhoneNo : " + myUser.getPhoneNo());
41 | System.out.println("Country : " + myUser.getCountry());
42 | System.out.println("---------------------------");
43 | }
44 | }
45 | }
46 |
47 | // Reads all CSV contents into memory (Not suitable for large CSV files)
48 | private static void readAllBeansAtOnce(CsvToBean csvToBean) {
49 | List myUsers = csvToBean.parse();
50 |
51 | for (MyUser myUser : myUsers) {
52 | System.out.println("Name : " + myUser.getName());
53 | System.out.println("Email : " + myUser.getEmail());
54 | System.out.println("PhoneNo : " + myUser.getPhoneNo());
55 | System.out.println("Country : " + myUser.getCountry());
56 | System.out.println("---------------------------");
57 | }
58 | }
59 | }
60 |
61 |
62 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/src/main/java/OpenCSVReadAndParseToBean.java:
--------------------------------------------------------------------------------
1 | import com.opencsv.bean.CsvToBean;
2 | import com.opencsv.bean.CsvToBeanBuilder;
3 |
4 | import java.io.IOException;
5 | import java.io.Reader;
6 | import java.nio.file.Files;
7 | import java.nio.file.Paths;
8 | import java.util.Iterator;
9 | import java.util.List;
10 |
11 | /**
12 | * Created by rajeevkumarsingh on 25/09/17.
13 | */
14 | public class OpenCSVReadAndParseToBean {
15 | private static final String SAMPLE_CSV_FILE_PATH = "./users-with-header.csv";
16 |
17 | public static void main(String[] args) throws IOException {
18 |
19 | try (
20 | Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
21 | ) {
22 | CsvToBean csvToBean = new CsvToBeanBuilder(reader)
23 | .withType(CSVUser.class)
24 | .withIgnoreLeadingWhiteSpace(true)
25 | .build();
26 |
27 | Iterator csvUserIterator = csvToBean.iterator();
28 |
29 | while (csvUserIterator.hasNext()) {
30 | CSVUser csvUser = csvUserIterator.next();
31 | System.out.println("Name : " + csvUser.getName());
32 | System.out.println("Email : " + csvUser.getEmail());
33 | System.out.println("PhoneNo : " + csvUser.getPhoneNo());
34 | System.out.println("Country : " + csvUser.getCountry());
35 | System.out.println("==========================");
36 | }
37 | }
38 | }
39 |
40 | // Reads all CSV contents into memory (Not suitable for large CSV files)
41 | private static void readAllBeansAtOnce(CsvToBean csvToBean) {
42 | List csvUsers = csvToBean.parse();
43 |
44 | for(CSVUser csvUser: csvUsers) {
45 | System.out.println("Name : " + csvUser.getName());
46 | System.out.println("Email : " + csvUser.getEmail());
47 | System.out.println("PhoneNo : " + csvUser.getPhoneNo());
48 | System.out.println("Country : " + csvUser.getCountry());
49 | System.out.println("==========================");
50 | }
51 | }
52 | }
53 |
54 |
55 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/src/main/java/OpenCSVReader.java:
--------------------------------------------------------------------------------
1 | import com.opencsv.CSVReader;
2 |
3 | import java.io.IOException;
4 | import java.io.Reader;
5 | import java.nio.file.Files;
6 | import java.nio.file.Paths;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by rajeevkumarsingh on 25/09/17.
11 | */
12 |
13 | public class OpenCSVReader {
14 | private static final String SAMPLE_CSV_FILE_PATH = "users.csv";
15 |
16 | public static void main(String[] args) throws IOException {
17 | readRecordsOneByOne();
18 | readAllRecordsAtOnce();
19 | }
20 |
21 | private static void readRecordsOneByOne() throws IOException {
22 | try (
23 | Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
24 | CSVReader csvReader = new CSVReader(reader);
25 | ) {
26 | // Reading Records One by One in a String array
27 | String[] nextRecord;
28 | while ((nextRecord = csvReader.readNext()) != null) {
29 | System.out.println("Name : " + nextRecord[0]);
30 | System.out.println("Email : " + nextRecord[1]);
31 | System.out.println("Phone : " + nextRecord[2]);
32 | System.out.println("Country : " + nextRecord[3]);
33 | System.out.println("==========================");
34 | }
35 | }
36 | }
37 |
38 | private static void readAllRecordsAtOnce() throws IOException {
39 | try (
40 | Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
41 | CSVReader csvReader = new CSVReader(reader);
42 | ) {
43 | // Reading All Records at once into a List
44 | List records = csvReader.readAll();
45 | for (String[] record : records) {
46 | System.out.println("Name : " + record[0]);
47 | System.out.println("Email : " + record[1]);
48 | System.out.println("Phone : " + record[2]);
49 | System.out.println("Country : " + record[3]);
50 | System.out.println("---------------------------");
51 | }
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/src/main/java/OpenCSVWriter.java:
--------------------------------------------------------------------------------
1 | import com.opencsv.CSVWriter;
2 | import com.opencsv.bean.StatefulBeanToCsv;
3 | import com.opencsv.bean.StatefulBeanToCsvBuilder;
4 | import com.opencsv.exceptions.CsvDataTypeMismatchException;
5 | import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
6 |
7 | import java.io.IOException;
8 | import java.io.Writer;
9 | import java.nio.file.Files;
10 | import java.nio.file.Paths;
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | /**
15 | * Created by rajeevkumarsingh on 25/09/17.
16 | */
17 | public class OpenCSVWriter {
18 | private static final String STRING_ARRAY_SAMPLE = "./string-array-sample.csv";
19 | private static final String OBJECT_LIST_SAMPLE = "./object-list-sample.csv";
20 |
21 | public static void main(String[] args) throws IOException,
22 | CsvDataTypeMismatchException,
23 | CsvRequiredFieldEmptyException {
24 |
25 | writeFromArrayOfStrings();
26 | writeFromListOfObjects();
27 | }
28 |
29 |
30 | private static void writeFromArrayOfStrings() throws IOException {
31 | try (
32 | Writer writer = Files.newBufferedWriter(Paths.get(STRING_ARRAY_SAMPLE));
33 |
34 | CSVWriter csvWriter = new CSVWriter(writer,
35 | CSVWriter.DEFAULT_SEPARATOR,
36 | CSVWriter.NO_QUOTE_CHARACTER,
37 | CSVWriter.DEFAULT_ESCAPE_CHARACTER,
38 | CSVWriter.DEFAULT_LINE_END);
39 | ) {
40 | String[] headerRecord = {"Name", "Email", "Phone", "Country"};
41 | csvWriter.writeNext(headerRecord);
42 |
43 | csvWriter.writeNext(new String[]{"Sundar Pichai ♥", "sundar.pichai@gmail.com", "+1-1111111111", "India"});
44 | csvWriter.writeNext(new String[]{"Satya Nadella", "satya.nadella@outlook.com", "+1-1111111112", "India"});
45 | }
46 | }
47 |
48 | private static void writeFromListOfObjects() throws IOException,
49 | CsvDataTypeMismatchException,
50 | CsvRequiredFieldEmptyException {
51 |
52 | try (
53 | Writer writer = Files.newBufferedWriter(Paths.get(STRING_ARRAY_SAMPLE));
54 | ) {
55 | StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
56 | .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
57 | .build();
58 |
59 | List myUsers = new ArrayList<>();
60 | myUsers.add(new MyUser("Sundar Pichai ♥", "sundar.pichai@gmail.com", "+1-1111111111", "India"));
61 | myUsers.add(new MyUser("Satya Nadella", "satya.nadella@outlook.com", "+1-1111111112", "India"));
62 |
63 | beanToCsv.write(myUsers);
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/string-array-sample.csv:
--------------------------------------------------------------------------------
1 | Name,Email,Phone,Country
2 | Sundar Pichai ♥,sundar.pichai@gmail.com,+1-1111111111,India
3 | Satya Nadella,satya.nadella@outlook.com,+1-1111111112,India
4 |
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/users-with-header.csv:
--------------------------------------------------------------------------------
1 | name,email,phone,country
2 | Rajeev Kumar Singh ♥,rajeevs@example.com,+91-9999999999,India
3 | Sachin Tendulkar,sachin@example.com,+91-9999999998,India
4 | Barak Obama,barak.obama@example.com,+1-1111111111,United States
5 | Donald Trump,donald.trump@example.com,+1-2222222222,United States
--------------------------------------------------------------------------------
/java-csv-file-handling-with-opencsv/users.csv:
--------------------------------------------------------------------------------
1 | Rajeev Kumar Singh ♥,rajeevs@example.com,+91-9999999999,India
2 | Sachin Tendulkar,sachin@example.com,+91-9999999998,India
3 | Barak Obama,barak.obama@example.com,+1-1111111111,United States
4 | Donald Trump,donald.trump@example.com,+1-2222222222,United States
--------------------------------------------------------------------------------