1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.example;
17
18 import java.io.FileReader;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.supercsv.cellprocessor.Optional;
23 import org.supercsv.cellprocessor.ParseBool;
24 import org.supercsv.cellprocessor.ParseDate;
25 import org.supercsv.cellprocessor.ParseInt;
26 import org.supercsv.cellprocessor.constraint.LMinMax;
27 import org.supercsv.cellprocessor.constraint.NotNull;
28 import org.supercsv.cellprocessor.constraint.StrRegEx;
29 import org.supercsv.cellprocessor.constraint.UniqueHashCode;
30 import org.supercsv.cellprocessor.ift.CellProcessor;
31 import org.supercsv.io.CsvBeanReader;
32 import org.supercsv.io.CsvListReader;
33 import org.supercsv.io.CsvMapReader;
34 import org.supercsv.io.ICsvBeanReader;
35 import org.supercsv.io.ICsvListReader;
36 import org.supercsv.io.ICsvMapReader;
37 import org.supercsv.mock.CustomerBean;
38 import org.supercsv.prefs.CsvPreference;
39
40
41
42
43 public class Reading {
44
45 private static final String CSV_FILENAME = "src/test/resources/customers.csv";
46
47 private static final String VARIABLE_CSV_FILENAME = "src/test/resources/customerswithvariablecolumns.csv";
48
49 public static void main(String[] args) throws Exception {
50 readWithCsvBeanReader();
51 readWithCsvListReader();
52 readVariableColumnsWithCsvListReader();
53 readWithCsvMapReader();
54 partialReadWithCsvBeanReader();
55 partialReadWithCsvMapReader();
56 }
57
58
59
60
61
62
63
64 private static CellProcessor[] getProcessors() {
65
66 final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
67 StrRegEx.registerMessage(emailRegex, "must be a valid email address");
68
69 final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(),
70 new NotNull(),
71 new NotNull(),
72 new ParseDate("dd/MM/yyyy"),
73 new NotNull(),
74 new Optional(new ParseBool()),
75 new Optional(new ParseInt()),
76 new NotNull(),
77 new StrRegEx(emailRegex),
78 new LMinMax(0L, LMinMax.MAX_LONG)
79 };
80
81 return processors;
82 }
83
84
85
86
87 private static void readWithCsvBeanReader() throws Exception {
88
89 ICsvBeanReader beanReader = null;
90 try {
91 beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
92
93
94 final String[] header = beanReader.getHeader(true);
95 final CellProcessor[] processors = getProcessors();
96
97 CustomerBean customer;
98 while( (customer = beanReader.read(CustomerBean.class, header, processors)) != null ) {
99 System.out.println(String.format("lineNo=%s, rowNo=%s, customer=%s", beanReader.getLineNumber(),
100 beanReader.getRowNumber(), customer));
101 }
102
103 }
104 finally {
105 if( beanReader != null ) {
106 beanReader.close();
107 }
108 }
109 }
110
111
112
113
114 private static void readWithCsvListReader() throws Exception {
115
116 ICsvListReader listReader = null;
117 try {
118 listReader = new CsvListReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
119
120 listReader.getHeader(true);
121 final CellProcessor[] processors = getProcessors();
122
123 List<Object> customerList;
124 while( (customerList = listReader.read(processors)) != null ) {
125 System.out.println(String.format("lineNo=%s, rowNo=%s, customerList=%s", listReader.getLineNumber(),
126 listReader.getRowNumber(), customerList));
127 }
128
129 }
130 finally {
131 if( listReader != null ) {
132 listReader.close();
133 }
134 }
135 }
136
137
138
139
140
141
142
143 private static void readVariableColumnsWithCsvListReader() throws Exception {
144
145 final CellProcessor[] allProcessors = new CellProcessor[] { new UniqueHashCode(),
146 new NotNull(),
147 new NotNull(),
148 new ParseDate("dd/MM/yyyy") };
149
150 final CellProcessor[] noBirthDateProcessors = new CellProcessor[] { allProcessors[0],
151 allProcessors[1],
152 allProcessors[2] };
153
154 ICsvListReader listReader = null;
155 try {
156 listReader = new CsvListReader(new FileReader(VARIABLE_CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
157
158 listReader.getHeader(true);
159
160 while( (listReader.read()) != null ) {
161
162
163 final CellProcessor[] processors;
164 if( listReader.length() == noBirthDateProcessors.length ) {
165 processors = noBirthDateProcessors;
166 } else {
167 processors = allProcessors;
168 }
169
170 final List<Object> customerList = listReader.executeProcessors(processors);
171 System.out.println(String.format("lineNo=%s, rowNo=%s, columns=%s, customerList=%s",
172 listReader.getLineNumber(), listReader.getRowNumber(), customerList.size(), customerList));
173 }
174
175 }
176 finally {
177 if( listReader != null ) {
178 listReader.close();
179 }
180 }
181 }
182
183
184
185
186 private static void readWithCsvMapReader() throws Exception {
187
188 ICsvMapReader mapReader = null;
189 try {
190 mapReader = new CsvMapReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
191
192
193 final String[] header = mapReader.getHeader(true);
194 final CellProcessor[] processors = getProcessors();
195
196 Map<String, Object> customerMap;
197 while( (customerMap = mapReader.read(header, processors)) != null ) {
198 System.out.println(String.format("lineNo=%s, rowNo=%s, customerMap=%s", mapReader.getLineNumber(),
199 mapReader.getRowNumber(), customerMap));
200 }
201
202 }
203 finally {
204 if( mapReader != null ) {
205 mapReader.close();
206 }
207 }
208 }
209
210
211
212
213 private static void partialReadWithCsvBeanReader() throws Exception {
214
215 ICsvBeanReader beanReader = null;
216 try {
217 beanReader = new CsvBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
218
219 beanReader.getHeader(true);
220
221
222 final String[] header = new String[] { "customerNo", "firstName", "lastName", null, null, null, null, null,
223 null, null };
224
225
226 final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
227 new NotNull(), null, null, null, null, null, null, null };
228
229 CustomerBean customer;
230 while( (customer = beanReader.read(CustomerBean.class, header, processors)) != null ) {
231 System.out.println(String.format("lineNo=%s, rowNo=%s, customer=%s", beanReader.getLineNumber(),
232 beanReader.getRowNumber(), customer));
233 }
234
235 }
236 finally {
237 if( beanReader != null ) {
238 beanReader.close();
239 }
240 }
241 }
242
243
244
245
246 private static void partialReadWithCsvMapReader() throws Exception {
247
248 ICsvMapReader mapReader = null;
249 try {
250 mapReader = new CsvMapReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
251
252 mapReader.getHeader(true);
253
254
255 final String[] header = new String[] { "customerNo", "firstName", "lastName", null, null, null, null, null,
256 null, null };
257
258
259 final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), new NotNull(),
260 new NotNull(), new NotNull(), new NotNull(), new Optional(), new Optional(), new NotNull(),
261 new NotNull(), new LMinMax(0L, LMinMax.MAX_LONG) };
262
263 Map<String, Object> customerMap;
264 while( (customerMap = mapReader.read(header, processors)) != null ) {
265 System.out.println(String.format("lineNo=%s, rowNo=%s, customerMap=%s", mapReader.getLineNumber(),
266 mapReader.getRowNumber(), customerMap));
267 }
268
269 }
270 finally {
271 if( mapReader != null ) {
272 mapReader.close();
273 }
274 }
275 }
276
277 }