BAEL-1484: Synchronous OpenCSV (#3784)

* BAEL-1484: Synchronous OpenCSV

* corrected pom.xml and package

* Refactored for future async example

* Resource Link
This commit is contained in:
Adam InTae Gerard 2018-04-11 23:58:55 -07:00 committed by Eugen
parent 69842b7dea
commit 8cc218de70
26 changed files with 638 additions and 2 deletions

View File

@ -63,6 +63,7 @@
- [A Docker Guide for Java](http://www.baeldung.com/docker-java-api)
- [Exceptions in Netty](http://www.baeldung.com/netty-exception-handling)
- [Creating and Configuring Jetty 9 Server in Java](http://www.baeldung.com/jetty-java-programmatic)
- [Introduction To OpenCSV](http://www.baeldung.com/opencsv)
- [Introduction to Akka Actors in Java] (http://www.baeldung.com/akka-actors-java)
- [Asynchronous HTTP with async-http-client in Java](https://github.com/eugenp/tutorials/tree/master/libraries)
- [Introduction to Smooks](http://www.baeldung.com/smooks)

View File

@ -43,6 +43,12 @@
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
@ -858,9 +864,9 @@
<tomcat.version>8.5.24</tomcat.version>
<async.http.client.version>2.2.0</async.http.client.version>
<infinispan.version>9.1.5.Final</infinispan.version>
<opencsv.version>4.1</opencsv.version>
<unirest.version>1.4.9</unirest.version>
<commons-codec-version>1.10.L001</commons-codec-version>
<jets3t-version>0.9.4.0006L</jets3t-version>
</properties>
</project>
</project>

View File

@ -0,0 +1,108 @@
package com.baeldung.opencsv;
import com.baeldung.opencsv.beans.NamedColumnBean;
import com.baeldung.opencsv.beans.SimplePositionBean;
import com.baeldung.opencsv.examples.sync.BeanExamples;
import com.baeldung.opencsv.examples.sync.CsvReaderExamples;
import com.baeldung.opencsv.examples.sync.CsvWriterExamples;
import com.baeldung.opencsv.helpers.Helpers;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
public class Application {
/*
* Bean Examples.
*/
public static String simpleSyncPositionBeanExample() {
Path path = null;
try {
path = Helpers.twoColumnCsvPath();
} catch (Exception ex) {
Helpers.err(ex);
}
return BeanExamples.beanBuilderExample(path, SimplePositionBean.class).toString();
}
public static String namedSyncColumnBeanExample() {
Path path = null;
try {
path = Helpers.namedColumnCsvPath();
} catch (Exception ex) {
Helpers.err(ex);
}
return BeanExamples.beanBuilderExample(path, NamedColumnBean.class).toString();
}
public static String writeSyncCsvFromBeanExample() {
Path path = null;
try {
path = Helpers.fileOutBeanPath();
} catch (Exception ex) {
Helpers.err(ex);
}
return BeanExamples.writeCsvFromBean(path);
}
/*
* CSV Reader Examples.
*/
public static String oneByOneSyncExample() {
Reader reader = null;
try {
reader = Files.newBufferedReader(Helpers.twoColumnCsvPath());
} catch (Exception ex) {
Helpers.err(ex);
}
return CsvReaderExamples.oneByOne(reader).toString();
}
public static String readAllSyncExample() {
Reader reader = null;
try {
reader = Files.newBufferedReader(Helpers.twoColumnCsvPath());
} catch (Exception ex) {
Helpers.err(ex);
}
return CsvReaderExamples.readAll(reader).toString();
}
/*
* CSV Writer Examples.
*/
public static String csvWriterSyncOneByOne() {
Path path = null;
try {
path = Helpers.fileOutOnePath();
} catch (Exception ex) {
Helpers.err(ex);
}
return CsvWriterExamples.csvWriterOneByOne(Helpers.fourColumnCsvString(), path);
}
public static String csvWriterSyncAll() {
Path path = null;
try {
path = Helpers.fileOutAllPath();
} catch (Exception ex) {
Helpers.err(ex);
}
return CsvWriterExamples.csvWriterAll(Helpers.fourColumnCsvString(), path);
}
public static void main(String[] args) {
simpleSyncPositionBeanExample();
namedSyncColumnBeanExample();
writeSyncCsvFromBeanExample();
oneByOneSyncExample();
readAllSyncExample();
csvWriterSyncOneByOne();
csvWriterSyncAll();
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.opencsv;
public class Constants {
public static final String GENERIC_EXCEPTION = "EXCEPTION ENCOUNTERED: ";
public static final String GENERIC_SUCCESS = "SUCCESS";
public static final String TWO_COLUMN_CSV = "csv/twoColumn.csv";
public static final String FOUR_COLUMN_CSV = "csv/fourColumn.csv";
public static final String NAMED_COLUMN_CSV = "csv/namedColumn.csv";
public static final String CSV_All = "csv/writtenAll.csv";
public static final String CSV_BEAN = "csv/writtenBean.csv";
public static final String CSV_ONE = "csv/writtenOneByOne.csv";
}

View File

@ -0,0 +1,3 @@
package com.baeldung.opencsv.beans;
public class CsvBean { }

View File

@ -0,0 +1,31 @@
package com.baeldung.opencsv.beans;
import com.opencsv.bean.CsvBindByName;
public class NamedColumnBean extends CsvBean {
@CsvBindByName(column = "name")
private String name;
//Automatically infer column name as Age
@CsvBindByName
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.opencsv.beans;
import com.opencsv.bean.CsvBindByPosition;
public class SimplePositionBean extends CsvBean {
@CsvBindByPosition(position = 0)
private String exampleColOne;
@CsvBindByPosition(position = 1)
private String exampleColTwo;
public String getExampleColOne() {
return exampleColOne;
}
private void setExampleColOne(String exampleColOne) {
this.exampleColOne = exampleColOne;
}
public String getExampleColTwo() {
return exampleColTwo;
}
private void setExampleCsvTwo (String exampleColTwo) {
this.exampleColTwo = exampleColTwo;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.opencsv.beans;
public class WriteExampleBean extends CsvBean {
private String colA;
private String colB;
private String colC;
public WriteExampleBean(String colA, String colB, String colC) {
this.colA = colA;
this.colB = colB;
this.colC = colC;
}
public String getColA() {
return colA;
}
public void setColA(String colA) {
this.colA = colA;
}
public String getColB() {
return colB;
}
public void setColB(String colB) {
this.colB = colB;
}
public String getColC() {
return colC;
}
public void setColC(String colC) {
this.colC = colC;
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.opencsv.examples.sync;
import com.baeldung.opencsv.beans.CsvBean;
import com.baeldung.opencsv.beans.WriteExampleBean;
import com.baeldung.opencsv.helpers.Helpers;
import com.baeldung.opencsv.pojos.CsvTransfer;
import com.opencsv.CSVWriter;
import com.opencsv.bean.*;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class BeanExamples {
public static List<CsvBean> beanBuilderExample(Path path, Class clazz) {
CsvTransfer csvTransfer = new CsvTransfer();
try {
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
ms.setType(clazz);
Reader reader = Files.newBufferedReader(path);
CsvToBean cb = new CsvToBeanBuilder(reader)
.withType(clazz)
.withMappingStrategy(ms)
.build();
csvTransfer.setCsvList(cb.parse());
reader.close();
} catch (Exception ex) {
Helpers.err(ex);
}
return csvTransfer.getCsvList();
}
public static String writeCsvFromBean(Path path) {
try {
Writer writer = new FileWriter(path.toString());
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer)
.withSeparator(CSVWriter.DEFAULT_SEPARATOR)
.build();
List<CsvBean> list = new ArrayList<>();
list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd"));
list.add(new WriteExampleBean("Test2", "ipso", "facto"));
sbc.write(list);
writer.close();
} catch (Exception ex) {
Helpers.err(ex);
}
return Helpers.readFile(path);
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.opencsv.examples.sync;
import com.baeldung.opencsv.helpers.Helpers;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
public class CsvReaderExamples {
public static List<String[]> readAll(Reader reader) {
CSVParser parser = new CSVParserBuilder()
.withSeparator(',')
.withIgnoreQuotations(true)
.build();
CSVReader csvReader = new CSVReaderBuilder(reader)
.withSkipLines(0)
.withCSVParser(parser)
.build();
List<String[]> list = new ArrayList<>();
try {
list = csvReader.readAll();
reader.close();
csvReader.close();
} catch (Exception ex) {
Helpers.err(ex);
}
return list;
}
public static List<String[]> oneByOne(Reader reader) {
List<String[]> list = new ArrayList<>();
try {
CSVParser parser = new CSVParserBuilder()
.withSeparator(',')
.withIgnoreQuotations(true)
.build();
CSVReader csvReader = new CSVReaderBuilder(reader)
.withSkipLines(0)
.withCSVParser(parser)
.build();
String[] line;
while ((line = csvReader.readNext()) != null) {
list.add(line);
}
reader.close();
csvReader.close();
} catch (Exception ex) {
Helpers.err(ex);
}
return list;
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.opencsv.examples.sync;
import com.baeldung.opencsv.helpers.Helpers;
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.nio.file.Path;
import java.util.List;
public class CsvWriterExamples {
public static String csvWriterOneByOne(List<String[]> stringArray, Path path) {
try {
CSVWriter writer = new CSVWriter(new FileWriter(path.toString()));
for (String[] array : stringArray) {
writer.writeNext(array);
}
writer.close();
} catch (Exception ex) {
Helpers.err(ex);
}
return Helpers.readFile(path);
}
public static String csvWriterAll(List<String[]> stringArray, Path path) {
try {
CSVWriter writer = new CSVWriter(new FileWriter(path.toString()));
writer.writeAll(stringArray);
writer.close();
} catch (Exception ex) {
Helpers.err(ex);
}
return Helpers.readFile(path);
}
}

View File

@ -0,0 +1,108 @@
package com.baeldung.opencsv.helpers;
import com.baeldung.opencsv.Constants;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Helpers {
/**
* Write Files
*/
public static Path fileOutAllPath() throws URISyntaxException {
URI uri = ClassLoader.getSystemResource(Constants.CSV_All).toURI();
return Paths.get(uri);
}
public static Path fileOutBeanPath() throws URISyntaxException {
URI uri = ClassLoader.getSystemResource(Constants.CSV_BEAN).toURI();
return Paths.get(uri);
}
public static Path fileOutOnePath() throws URISyntaxException {
URI uri = ClassLoader.getSystemResource(Constants.CSV_ONE).toURI();
return Paths.get(uri);
}
/**
* Read Files
*/
public static Path twoColumnCsvPath() throws URISyntaxException {
URI uri = ClassLoader.getSystemResource(Constants.TWO_COLUMN_CSV).toURI();
return Paths.get(uri);
}
public static Path fourColumnCsvPath() throws URISyntaxException {
URI uri = ClassLoader.getSystemResource(Constants.FOUR_COLUMN_CSV).toURI();
return Paths.get(uri);
}
public static Path namedColumnCsvPath() throws URISyntaxException {
URI uri = ClassLoader.getSystemResource(Constants.NAMED_COLUMN_CSV).toURI();
return Paths.get(uri);
}
/**
* Simple File Reader
*/
public static String readFile(Path path) {
String response = "";
try {
FileReader fr = new FileReader(path.toString());
BufferedReader br = new BufferedReader(fr);
String strLine;
StringBuffer sb = new StringBuffer();
while ((strLine = br.readLine()) != null) {
sb.append(strLine);
}
response = sb.toString();
System.out.println(response);
fr.close();
br.close();
} catch (Exception ex) {
Helpers.err(ex);
}
return response;
}
/**
* Dummy Data for Writing.
*/
public static List<String[]> twoColumnCsvString() {
List<String[]> list = new ArrayList<>();
list.add(new String[]{"ColA", "ColB"});
list.add(new String[]{"A", "B"});
return list;
}
public static List<String[]> fourColumnCsvString() {
List<String[]> list = new ArrayList<>();
list.add(new String[]{"ColA", "ColB", "ColC", "ColD"});
list.add(new String[]{"A", "B", "A", "B"});
list.add(new String[]{"BB", "AB", "AA", "B"});
return list;
}
/**
* Message Helpers
*/
public static void print(String msg) {
System.out.println(msg);
}
public static void err(Exception ex) {
System.out.println(Constants.GENERIC_EXCEPTION + " " + ex);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.opencsv.pojos;
import com.baeldung.opencsv.beans.CsvBean;
import java.util.ArrayList;
import java.util.List;
public class CsvTransfer {
private List<String[]> csvStringList;
private List<CsvBean> csvList;
public CsvTransfer() {}
public List<String[]> getCsvStringList() {
if (csvStringList != null) return csvStringList;
return new ArrayList<String[]>();
}
public void addLine(String[] line) {
if (this.csvList == null) this.csvStringList = new ArrayList<>();
this.csvStringList.add(line);
}
public void setCsvStringList(List<String[]> csvStringList) {
this.csvStringList = csvStringList;
}
public void setCsvList(List<CsvBean> csvList) {
this.csvList = csvList;
}
public List<CsvBean> getCsvList() {
if (csvList != null) return csvList;
return new ArrayList<CsvBean>();
}
}

View File

@ -0,0 +1,5 @@
ColA,ColB,ColC,ColD
A,B,B,B
C,D,W,W
G,G,E,E
G,F,Q,Q
1 ColA ColB ColC ColD
2 A B B B
3 C D W W
4 G G E E
5 G F Q Q

View File

@ -0,0 +1,5 @@
name,age
adam,1000
martin,27
gigi,41
seraphine,30
1 name age
2 adam 1000
3 martin 27
4 gigi 41
5 seraphine 30

View File

@ -0,0 +1,5 @@
ColA,ColB
A,B
C,D
G,G
G,F
1 ColA ColB
2 A B
3 C D
4 G G
5 G F

View File

@ -0,0 +1,66 @@
package com.baeldung.opencsv;
import com.baeldung.opencsv.helpers.Helpers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class OpenCsvTest {
private Object testReadCsv(Object result) {
assert (result != null);
assert (result instanceof String);
assert (!((String) result).isEmpty());
System.out.println(result);
return result;
}
private Object testWriteCsv(Object result) {
assert (result instanceof String);
assert (!((String) result).isEmpty());
return result;
}
@Before
public void setup() {
}
@Test
public void positionExampleTest() {
testReadCsv(Application.simpleSyncPositionBeanExample());
}
@Test
public void namedColumnExampleTest() {
testReadCsv(Application.namedSyncColumnBeanExample());
}
@Test
public void writeCsvUsingBeanBuilderTest() {
testWriteCsv(Application.writeSyncCsvFromBeanExample());
}
@Test
public void oneByOneExampleTest() {
testReadCsv(Application.oneByOneSyncExample());
}
@Test
public void readAllExampleTest() {
testReadCsv(Application.readAllSyncExample());
}
@Test
public void csvWriterOneByOneTest() {
testWriteCsv(Application.csvWriterSyncOneByOne());
}
@Test
public void csvWriterAllTest() {
testWriteCsv(Application.csvWriterSyncAll());
}
@After
public void close() {
}
}

View File

@ -0,0 +1,5 @@
ColA,ColB,ColC,ColD
A,B,B,B
C,D,W,W
G,G,E,E
G,F,Q,Q
1 ColA ColB ColC ColD
2 A B B B
3 C D W W
4 G G E E
5 G F Q Q

View File

@ -0,0 +1,5 @@
name,age
adam,1000
martin,27
gigi,41
seraphine,30
1 name age
2 adam 1000
3 martin 27
4 gigi 41
5 seraphine 30

View File

@ -0,0 +1,5 @@
ColA,ColB
A,B
C,D
G,G
G,F
1 ColA ColB
2 A B
3 C D
4 G G
5 G F