|
| 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 | +} |
0 commit comments