Skip to content

Commit 8cc218d

Browse files
ThoughtscriptEugen
authored andcommitted
BAEL-1484: Synchronous OpenCSV (eugenp#3784)
* BAEL-1484: Synchronous OpenCSV * corrected pom.xml and package * Refactored for future async example * Resource Link
1 parent 69842b7 commit 8cc218d

26 files changed

Lines changed: 638 additions & 2 deletions

‎libraries/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- [A Docker Guide for Java](http://www.baeldung.com/docker-java-api)
6464
- [Exceptions in Netty](http://www.baeldung.com/netty-exception-handling)
6565
- [Creating and Configuring Jetty 9 Server in Java](http://www.baeldung.com/jetty-java-programmatic)
66+
- [Introduction To OpenCSV](http://www.baeldung.com/opencsv)
6667
- [Introduction to Akka Actors in Java] (http://www.baeldung.com/akka-actors-java)
6768
- [Asynchronous HTTP with async-http-client in Java](https://github.com/eugenp/tutorials/tree/master/libraries)
6869
- [Introduction to Smooks](http://www.baeldung.com/smooks)

‎libraries/pom.xml‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<artifactId>cglib</artifactId>
4444
<version>${cglib.version}</version>
4545
</dependency>
46+
<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
47+
<dependency>
48+
<groupId>com.opencsv</groupId>
49+
<artifactId>opencsv</artifactId>
50+
<version>${opencsv.version}</version>
51+
</dependency>
4652
<dependency>
4753
<groupId>commons-beanutils</groupId>
4854
<artifactId>commons-beanutils</artifactId>
@@ -858,9 +864,9 @@
858864
<tomcat.version>8.5.24</tomcat.version>
859865
<async.http.client.version>2.2.0</async.http.client.version>
860866
<infinispan.version>9.1.5.Final</infinispan.version>
867+
<opencsv.version>4.1</opencsv.version>
861868
<unirest.version>1.4.9</unirest.version>
862869
<commons-codec-version>1.10.L001</commons-codec-version>
863870
<jets3t-version>0.9.4.0006L</jets3t-version>
864871
</properties>
865-
866-
</project>
872+
</project>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.baeldung.opencsv;
2+
3+
import com.baeldung.opencsv.beans.NamedColumnBean;
4+
import com.baeldung.opencsv.beans.SimplePositionBean;
5+
import com.baeldung.opencsv.examples.sync.BeanExamples;
6+
import com.baeldung.opencsv.examples.sync.CsvReaderExamples;
7+
import com.baeldung.opencsv.examples.sync.CsvWriterExamples;
8+
import com.baeldung.opencsv.helpers.Helpers;
9+
10+
import java.io.Reader;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
14+
public class Application {
15+
16+
/*
17+
* Bean Examples.
18+
*/
19+
20+
public static String simpleSyncPositionBeanExample() {
21+
Path path = null;
22+
try {
23+
path = Helpers.twoColumnCsvPath();
24+
} catch (Exception ex) {
25+
Helpers.err(ex);
26+
}
27+
return BeanExamples.beanBuilderExample(path, SimplePositionBean.class).toString();
28+
}
29+
30+
public static String namedSyncColumnBeanExample() {
31+
Path path = null;
32+
try {
33+
path = Helpers.namedColumnCsvPath();
34+
} catch (Exception ex) {
35+
Helpers.err(ex);
36+
}
37+
return BeanExamples.beanBuilderExample(path, NamedColumnBean.class).toString();
38+
}
39+
40+
public static String writeSyncCsvFromBeanExample() {
41+
Path path = null;
42+
try {
43+
path = Helpers.fileOutBeanPath();
44+
} catch (Exception ex) {
45+
Helpers.err(ex);
46+
}
47+
return BeanExamples.writeCsvFromBean(path);
48+
}
49+
50+
/*
51+
* CSV Reader Examples.
52+
*/
53+
54+
public static String oneByOneSyncExample() {
55+
Reader reader = null;
56+
try {
57+
reader = Files.newBufferedReader(Helpers.twoColumnCsvPath());
58+
} catch (Exception ex) {
59+
Helpers.err(ex);
60+
}
61+
return CsvReaderExamples.oneByOne(reader).toString();
62+
}
63+
64+
public static String readAllSyncExample() {
65+
Reader reader = null;
66+
try {
67+
reader = Files.newBufferedReader(Helpers.twoColumnCsvPath());
68+
} catch (Exception ex) {
69+
Helpers.err(ex);
70+
}
71+
return CsvReaderExamples.readAll(reader).toString();
72+
}
73+
74+
/*
75+
* CSV Writer Examples.
76+
*/
77+
78+
79+
public static String csvWriterSyncOneByOne() {
80+
Path path = null;
81+
try {
82+
path = Helpers.fileOutOnePath();
83+
} catch (Exception ex) {
84+
Helpers.err(ex);
85+
}
86+
return CsvWriterExamples.csvWriterOneByOne(Helpers.fourColumnCsvString(), path);
87+
}
88+
89+
public static String csvWriterSyncAll() {
90+
Path path = null;
91+
try {
92+
path = Helpers.fileOutAllPath();
93+
} catch (Exception ex) {
94+
Helpers.err(ex);
95+
}
96+
return CsvWriterExamples.csvWriterAll(Helpers.fourColumnCsvString(), path);
97+
}
98+
99+
public static void main(String[] args) {
100+
simpleSyncPositionBeanExample();
101+
namedSyncColumnBeanExample();
102+
writeSyncCsvFromBeanExample();
103+
oneByOneSyncExample();
104+
readAllSyncExample();
105+
csvWriterSyncOneByOne();
106+
csvWriterSyncAll();
107+
}
108+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.opencsv;
2+
3+
public class Constants {
4+
5+
public static final String GENERIC_EXCEPTION = "EXCEPTION ENCOUNTERED: ";
6+
public static final String GENERIC_SUCCESS = "SUCCESS";
7+
8+
public static final String TWO_COLUMN_CSV = "csv/twoColumn.csv";
9+
public static final String FOUR_COLUMN_CSV = "csv/fourColumn.csv";
10+
public static final String NAMED_COLUMN_CSV = "csv/namedColumn.csv";
11+
12+
public static final String CSV_All = "csv/writtenAll.csv";
13+
public static final String CSV_BEAN = "csv/writtenBean.csv";
14+
public static final String CSV_ONE = "csv/writtenOneByOne.csv";
15+
16+
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.opencsv.beans;
2+
3+
public class CsvBean { }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.opencsv.beans;
2+
3+
import com.opencsv.bean.CsvBindByName;
4+
5+
public class NamedColumnBean extends CsvBean {
6+
7+
@CsvBindByName(column = "name")
8+
private String name;
9+
10+
//Automatically infer column name as Age
11+
@CsvBindByName
12+
private int age;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public int getAge() {
23+
return age;
24+
}
25+
26+
public void setAge(int age) {
27+
this.age = age;
28+
}
29+
30+
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.opencsv.beans;
2+
3+
import com.opencsv.bean.CsvBindByPosition;
4+
5+
public class SimplePositionBean extends CsvBean {
6+
7+
@CsvBindByPosition(position = 0)
8+
private String exampleColOne;
9+
10+
@CsvBindByPosition(position = 1)
11+
private String exampleColTwo;
12+
13+
public String getExampleColOne() {
14+
return exampleColOne;
15+
}
16+
17+
private void setExampleColOne(String exampleColOne) {
18+
this.exampleColOne = exampleColOne;
19+
}
20+
21+
public String getExampleColTwo() {
22+
return exampleColTwo;
23+
}
24+
25+
private void setExampleCsvTwo (String exampleColTwo) {
26+
this.exampleColTwo = exampleColTwo;
27+
}
28+
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.opencsv.beans;
2+
3+
public class WriteExampleBean extends CsvBean {
4+
5+
private String colA;
6+
7+
private String colB;
8+
9+
private String colC;
10+
11+
public WriteExampleBean(String colA, String colB, String colC) {
12+
this.colA = colA;
13+
this.colB = colB;
14+
this.colC = colC;
15+
}
16+
17+
public String getColA() {
18+
return colA;
19+
}
20+
21+
public void setColA(String colA) {
22+
this.colA = colA;
23+
}
24+
25+
public String getColB() {
26+
return colB;
27+
}
28+
29+
public void setColB(String colB) {
30+
this.colB = colB;
31+
}
32+
33+
public String getColC() {
34+
return colC;
35+
}
36+
37+
public void setColC(String colC) {
38+
this.colC = colC;
39+
}
40+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.opencsv.examples.sync;
2+
3+
import com.baeldung.opencsv.beans.CsvBean;
4+
import com.baeldung.opencsv.beans.WriteExampleBean;
5+
import com.baeldung.opencsv.helpers.Helpers;
6+
import com.baeldung.opencsv.pojos.CsvTransfer;
7+
import com.opencsv.CSVWriter;
8+
import com.opencsv.bean.*;
9+
10+
import java.io.FileWriter;
11+
import java.io.Reader;
12+
import java.io.Writer;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
public class BeanExamples {
19+
20+
public static List<CsvBean> beanBuilderExample(Path path, Class clazz) {
21+
CsvTransfer csvTransfer = new CsvTransfer();
22+
try {
23+
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
24+
ms.setType(clazz);
25+
26+
Reader reader = Files.newBufferedReader(path);
27+
CsvToBean cb = new CsvToBeanBuilder(reader)
28+
.withType(clazz)
29+
.withMappingStrategy(ms)
30+
.build();
31+
32+
csvTransfer.setCsvList(cb.parse());
33+
reader.close();
34+
35+
} catch (Exception ex) {
36+
Helpers.err(ex);
37+
}
38+
return csvTransfer.getCsvList();
39+
}
40+
41+
public static String writeCsvFromBean(Path path) {
42+
try {
43+
Writer writer = new FileWriter(path.toString());
44+
45+
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer)
46+
.withSeparator(CSVWriter.DEFAULT_SEPARATOR)
47+
.build();
48+
49+
List<CsvBean> list = new ArrayList<>();
50+
list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd"));
51+
list.add(new WriteExampleBean("Test2", "ipso", "facto"));
52+
53+
sbc.write(list);
54+
writer.close();
55+
56+
} catch (Exception ex) {
57+
Helpers.err(ex);
58+
}
59+
return Helpers.readFile(path);
60+
}
61+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.opencsv.examples.sync;
2+
3+
import com.baeldung.opencsv.helpers.Helpers;
4+
import com.opencsv.CSVParser;
5+
import com.opencsv.CSVParserBuilder;
6+
import com.opencsv.CSVReader;
7+
import com.opencsv.CSVReaderBuilder;
8+
9+
import java.io.Reader;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class CsvReaderExamples {
14+
15+
public static List<String[]> readAll(Reader reader) {
16+
17+
CSVParser parser = new CSVParserBuilder()
18+
.withSeparator(',')
19+
.withIgnoreQuotations(true)
20+
.build();
21+
22+
CSVReader csvReader = new CSVReaderBuilder(reader)
23+
.withSkipLines(0)
24+
.withCSVParser(parser)
25+
.build();
26+
27+
List<String[]> list = new ArrayList<>();
28+
try {
29+
list = csvReader.readAll();
30+
reader.close();
31+
csvReader.close();
32+
} catch (Exception ex) {
33+
Helpers.err(ex);
34+
}
35+
return list;
36+
}
37+
38+
public static List<String[]> oneByOne(Reader reader) {
39+
List<String[]> list = new ArrayList<>();
40+
try {
41+
CSVParser parser = new CSVParserBuilder()
42+
.withSeparator(',')
43+
.withIgnoreQuotations(true)
44+
.build();
45+
46+
CSVReader csvReader = new CSVReaderBuilder(reader)
47+
.withSkipLines(0)
48+
.withCSVParser(parser)
49+
.build();
50+
51+
String[] line;
52+
while ((line = csvReader.readNext()) != null) {
53+
list.add(line);
54+
}
55+
reader.close();
56+
csvReader.close();
57+
} catch (Exception ex) {
58+
Helpers.err(ex);
59+
}
60+
return list;
61+
}
62+
63+
}

0 commit comments

Comments
 (0)