View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Examples of reading CSV files.
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  	 * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. Empty
60  	 * columns are read as null (hence the NotNull() for mandatory columns).
61  	 * 
62  	 * @return the cell processors
63  	 */
64  	private static CellProcessor[] getProcessors() {
65  		
66  		final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+"; // just an example, not very robust!
67  		StrRegEx.registerMessage(emailRegex, "must be a valid email address");
68  		
69  		final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique)
70  			new NotNull(), // firstName
71  			new NotNull(), // lastName
72  			new ParseDate("dd/MM/yyyy"), // birthDate
73  			new NotNull(), // mailingAddress
74  			new Optional(new ParseBool()), // married
75  			new Optional(new ParseInt()), // numberOfKids
76  			new NotNull(), // favouriteQuote
77  			new StrRegEx(emailRegex), // email
78  			new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
79  		};
80  		
81  		return processors;
82  	}
83  	
84  	/**
85  	 * An example of reading using CsvBeanReader.
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  			// the header elements are used to map the values to the bean (names must match)
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 	 * An example of reading using CsvListReader.
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); // skip the header (can't be used with CsvListReader)
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 	 * An example of reading a file with a variable number of columns using CsvListReader. It demonstrates that you can
139 	 * still use cell processors, but you must execute them by calling the executeProcessors() method on the reader,
140 	 * instead of supplying processors to the read() method. In this scenario, the last column (birthDate) is sometimes
141 	 * missing.
142 	 */
143 	private static void readVariableColumnsWithCsvListReader() throws Exception {
144 		
145 		final CellProcessor[] allProcessors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique)
146 			new NotNull(), // firstName
147 			new NotNull(), // lastName
148 			new ParseDate("dd/MM/yyyy") }; // birthDate
149 		
150 		final CellProcessor[] noBirthDateProcessors = new CellProcessor[] { allProcessors[0], // customerNo
151 			allProcessors[1], // firstName
152 			allProcessors[2] }; // lastName
153 		
154 		ICsvListReader listReader = null;
155 		try {
156 			listReader = new CsvListReader(new FileReader(VARIABLE_CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
157 			
158 			listReader.getHeader(true); // skip the header (can't be used with CsvListReader)
159 			
160 			while( (listReader.read()) != null ) {
161 				
162 				// use different processors depending on the number of columns
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 	 * An example of reading using CsvMapReader.
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 			// the header columns are used as the keys to the Map
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 	 * An example of partial reading using CsvBeanReader.
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); // skip past the header (we're defining our own)
220 			
221 			// only map the first 3 columns - setting header elements to null means those columns are ignored
222 			final String[] header = new String[] { "customerNo", "firstName", "lastName", null, null, null, null, null,
223 				null, null };
224 			
225 			// no processing required for ignored columns
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 	 * An example of partial reading using CsvMapReader.
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); // skip past the header (we're defining our own)
253 			
254 			// only map the first 3 columns - setting header elements to null means those columns are ignored
255 			final String[] header = new String[] { "customerNo", "firstName", "lastName", null, null, null, null, null,
256 				null, null };
257 			
258 			// apply some constraints to ignored columns (just because we can)
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 }