diff --git a/libraries/README.md b/libraries/README.md index d13cd24bd8..7c06aa88ca 100644 --- a/libraries/README.md +++ b/libraries/README.md @@ -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) diff --git a/libraries/pom.xml b/libraries/pom.xml index 3a43c12c41..be2876ec3b 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -43,6 +43,12 @@ cglib ${cglib.version} + + + com.opencsv + opencsv + ${opencsv.version} + commons-beanutils commons-beanutils @@ -858,9 +864,9 @@ 8.5.24 2.2.0 9.1.5.Final + 4.1 1.4.9 1.10.L001 0.9.4.0006L - - + \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/opencsv/Application.java b/libraries/src/main/java/com/baeldung/opencsv/Application.java new file mode 100644 index 0000000000..fd7e4d82d4 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/Application.java @@ -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(); + } +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/Constants.java b/libraries/src/main/java/com/baeldung/opencsv/Constants.java new file mode 100644 index 0000000000..f7978acf24 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/Constants.java @@ -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"; + + +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/beans/CsvBean.java b/libraries/src/main/java/com/baeldung/opencsv/beans/CsvBean.java new file mode 100644 index 0000000000..af9caaae12 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/beans/CsvBean.java @@ -0,0 +1,3 @@ +package com.baeldung.opencsv.beans; + +public class CsvBean { } diff --git a/libraries/src/main/java/com/baeldung/opencsv/beans/NamedColumnBean.java b/libraries/src/main/java/com/baeldung/opencsv/beans/NamedColumnBean.java new file mode 100644 index 0000000000..d9e5f96333 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/beans/NamedColumnBean.java @@ -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; + } + + +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/beans/SimplePositionBean.java b/libraries/src/main/java/com/baeldung/opencsv/beans/SimplePositionBean.java new file mode 100644 index 0000000000..799006e2f9 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/beans/SimplePositionBean.java @@ -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; + } + +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/beans/WriteExampleBean.java b/libraries/src/main/java/com/baeldung/opencsv/beans/WriteExampleBean.java new file mode 100644 index 0000000000..f145f67f11 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/beans/WriteExampleBean.java @@ -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; + } +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/examples/sync/BeanExamples.java b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/BeanExamples.java new file mode 100644 index 0000000000..76d044ca60 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/BeanExamples.java @@ -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 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 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); + } +} \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/opencsv/examples/sync/CsvReaderExamples.java b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/CsvReaderExamples.java new file mode 100644 index 0000000000..bacfdcff3d --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/CsvReaderExamples.java @@ -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 readAll(Reader reader) { + + CSVParser parser = new CSVParserBuilder() + .withSeparator(',') + .withIgnoreQuotations(true) + .build(); + + CSVReader csvReader = new CSVReaderBuilder(reader) + .withSkipLines(0) + .withCSVParser(parser) + .build(); + + List list = new ArrayList<>(); + try { + list = csvReader.readAll(); + reader.close(); + csvReader.close(); + } catch (Exception ex) { + Helpers.err(ex); + } + return list; + } + + public static List oneByOne(Reader reader) { + List 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; + } + +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/examples/sync/CsvWriterExamples.java b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/CsvWriterExamples.java new file mode 100644 index 0000000000..6f52160ba8 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/examples/sync/CsvWriterExamples.java @@ -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 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 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); + } +} \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/opencsv/helpers/Helpers.java b/libraries/src/main/java/com/baeldung/opencsv/helpers/Helpers.java new file mode 100644 index 0000000000..9a46fbc495 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/helpers/Helpers.java @@ -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 twoColumnCsvString() { + List list = new ArrayList<>(); + list.add(new String[]{"ColA", "ColB"}); + list.add(new String[]{"A", "B"}); + return list; + } + + public static List fourColumnCsvString() { + List 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); + } +} diff --git a/libraries/src/main/java/com/baeldung/opencsv/pojos/CsvTransfer.java b/libraries/src/main/java/com/baeldung/opencsv/pojos/CsvTransfer.java new file mode 100644 index 0000000000..79366e3d20 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/opencsv/pojos/CsvTransfer.java @@ -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 csvStringList; + + private List csvList; + + public CsvTransfer() {} + + public List getCsvStringList() { + if (csvStringList != null) return csvStringList; + return new ArrayList(); + } + + public void addLine(String[] line) { + if (this.csvList == null) this.csvStringList = new ArrayList<>(); + this.csvStringList.add(line); + } + + public void setCsvStringList(List csvStringList) { + this.csvStringList = csvStringList; + } + + public void setCsvList(List csvList) { + this.csvList = csvList; + } + + public List getCsvList() { + if (csvList != null) return csvList; + return new ArrayList(); + } +} \ No newline at end of file diff --git a/libraries/src/main/resources/csv/fourColumn.csv b/libraries/src/main/resources/csv/fourColumn.csv new file mode 100644 index 0000000000..51a69fd1a0 --- /dev/null +++ b/libraries/src/main/resources/csv/fourColumn.csv @@ -0,0 +1,5 @@ +ColA,ColB,ColC,ColD +A,B,B,B +C,D,W,W +G,G,E,E +G,F,Q,Q diff --git a/libraries/src/main/resources/csv/namedColumn.csv b/libraries/src/main/resources/csv/namedColumn.csv new file mode 100644 index 0000000000..279b743600 --- /dev/null +++ b/libraries/src/main/resources/csv/namedColumn.csv @@ -0,0 +1,5 @@ +name,age +adam,1000 +martin,27 +gigi,41 +seraphine,30 \ No newline at end of file diff --git a/libraries/src/main/resources/csv/twoColumn.csv b/libraries/src/main/resources/csv/twoColumn.csv new file mode 100644 index 0000000000..3eab8e8a64 --- /dev/null +++ b/libraries/src/main/resources/csv/twoColumn.csv @@ -0,0 +1,5 @@ +ColA,ColB +A,B +C,D +G,G +G,F diff --git a/libraries/src/main/resources/csv/writtenAll.csv b/libraries/src/main/resources/csv/writtenAll.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/src/main/resources/csv/writtenBean.csv b/libraries/src/main/resources/csv/writtenBean.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/src/main/resources/csv/writtenOneByOne.csv b/libraries/src/main/resources/csv/writtenOneByOne.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/src/test/java/com/baeldung/opencsv/OpenCsvTest.java b/libraries/src/test/java/com/baeldung/opencsv/OpenCsvTest.java new file mode 100644 index 0000000000..394b3fba4f --- /dev/null +++ b/libraries/src/test/java/com/baeldung/opencsv/OpenCsvTest.java @@ -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() { + } +} \ No newline at end of file diff --git a/libraries/src/test/resources/csv/fourColumn.csv b/libraries/src/test/resources/csv/fourColumn.csv new file mode 100644 index 0000000000..51a69fd1a0 --- /dev/null +++ b/libraries/src/test/resources/csv/fourColumn.csv @@ -0,0 +1,5 @@ +ColA,ColB,ColC,ColD +A,B,B,B +C,D,W,W +G,G,E,E +G,F,Q,Q diff --git a/libraries/src/test/resources/csv/namedColumn.csv b/libraries/src/test/resources/csv/namedColumn.csv new file mode 100644 index 0000000000..279b743600 --- /dev/null +++ b/libraries/src/test/resources/csv/namedColumn.csv @@ -0,0 +1,5 @@ +name,age +adam,1000 +martin,27 +gigi,41 +seraphine,30 \ No newline at end of file diff --git a/libraries/src/test/resources/csv/twoColumn.csv b/libraries/src/test/resources/csv/twoColumn.csv new file mode 100644 index 0000000000..3eab8e8a64 --- /dev/null +++ b/libraries/src/test/resources/csv/twoColumn.csv @@ -0,0 +1,5 @@ +ColA,ColB +A,B +C,D +G,G +G,F diff --git a/libraries/src/test/resources/csv/writtenAll.csv b/libraries/src/test/resources/csv/writtenAll.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/src/test/resources/csv/writtenBean.csv b/libraries/src/test/resources/csv/writtenBean.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/src/test/resources/csv/writtenOneByOne.csv b/libraries/src/test/resources/csv/writtenOneByOne.csv new file mode 100644 index 0000000000..e69de29bb2