Super CSV.
CsvBeanReader
Dozer bean, setter bean, CsvBeanReader . , Person PersonAttribute beans no-args getters/seters, ( ).
Person bean :
public void setAddAttribute(PersonAttribute attribute){
if (attribs == null){
attribs = new ArrayList<PersonAttribute>();
}
attribs.add(attribute);
}
, PersonAttribute CSV, CSV.
package org.supercsv.example;
import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.util.CsvContext;
public class ParsePersonAttribute extends CellProcessorAdaptor {
private final String[] header;
public ParsePersonAttribute(final String[] header) {
this.header = header;
}
public Object execute(Object value, CsvContext context) {
if( value == null ) {
return null;
}
PersonAttribute attribute = new PersonAttribute();
attribute.setKey(header[context.getColumnNumber() - 1]);
attribute.setValue((String) value);
return attribute;
}
}
, , , :
:
package org.supercsv.example;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
public class ReadWithCsvBeanReader {
private static final String CSV =
"firstname, lastname, dog_name, fav_hat, fav_color\n"
+ "bill,smith,fido,porkpie,blue\n"
+ "james,smith,rover,bowler,purple";
private static final String CSV2 =
"firstname, lastname, car_type, floor_number\n"
+ "tom, collins, ford, 14\n" + "jim, jones, toyota, 120";
private static final int ATT_START_INDEX = 2;
spaces that aren't part of the data
private static final CsvPreference PREFS =
new CsvPreference.Builder(
CsvPreference.STANDARD_PREFERENCE)
.surroundingSpacesNeedQuotes(true).build();
public static void main(String[] args) throws IOException {
System.out.println("CsvBeanReader with first CSV input:");
readWithCsvBeanReader(new StringReader(CSV));
System.out.println("CsvBeanReader with second CSV input:");
readWithCsvBeanReader(new StringReader(CSV2));
}
private static void readWithCsvBeanReader(final Reader reader)
throws IOException {
ICsvBeanReader beanReader = null;
try {
beanReader = new CsvBeanReader(reader, PREFS);
final String[] header = beanReader.getHeader(true);
final String[] fieldMapping = new String[header.length];
final CellProcessor[] processors =
new CellProcessor[header.length];
for (int i = 0; i < header.length; i++) {
if (i < ATT_START_INDEX) {
fieldMapping[i] = header[i];
processors[i] = new NotNull();
} else {
fieldMapping[i] = "addAttribute";
processors[i] =
new Optional(new ParsePersonAttribute(header));
}
}
Person person;
while ((person = beanReader.read(Person.class, fieldMapping,
processors)) != null) {
System.out.println(String.format(
"lineNo=%s, rowNo=%s, person=%s",
beanReader.getLineNumber(), beanReader.getRowNumber(),
person));
}
} finally {
if (beanReader != null) {
beanReader.close();
}
}
}
}
( toString() beans):
CsvBeanReader with first CSV input:
lineNo=2, rowNo=2, person=Person [firstname=bill, lastname=smith, attribs=[PersonAttribute [key=dog_name, value=fido], PersonAttribute [key=fav_hat, value=porkpie], PersonAttribute [key=fav_color, value=blue]]]
lineNo=3, rowNo=3, person=Person [firstname=james, lastname=smith, attribs=[PersonAttribute [key=dog_name, value=rover], PersonAttribute [key=fav_hat, value=bowler], PersonAttribute [key=fav_color, value=purple]]]
CsvBeanReader with second CSV input:
lineNo=2, rowNo=2, person=Person [firstname=tom, lastname=collins, attribs=[PersonAttribute [key=car_type, value=ford], PersonAttribute [key=floor_number, value=14]]]
lineNo=3, rowNo=3, person=Person [firstname=jim, lastname=jones, attribs=[PersonAttribute [key=car_type, value=toyota], PersonAttribute [key=floor_number, value=120]]]
CsvDozerBeanReader
bean, CsvDozerBeanReader Super CSV Dozer Extension, . .
CsvDozerBeanReader. , CsvBeanReader, :
:
package org.supercsv.example;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.dozer.CsvDozerBeanReader;
import org.supercsv.io.dozer.ICsvDozerBeanReader;
import org.supercsv.prefs.CsvPreference;
public class ReadWithCsvDozerBeanReader {
private static final String CSV =
"firstname, lastname, dog_name, fav_hat, fav_color\n"
+ "bill,smith,fido,porkpie,blue\n"
+ "james,smith,rover,bowler,purple";
private static final String CSV2 =
"firstname, lastname, car_type, floor_number\n"
+ "tom, collins, ford, 14\n"
+ "jim, jones, toyota, 120";
private static final int ATT_START_INDEX = 2;
private static final CsvPreference PREFS = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE)
.surroundingSpacesNeedQuotes(true).build();
public static void main(String[] args) throws IOException {
System.out.println("CsvDozerBeanReader with first CSV input:");
readWithCsvDozerBeanReader(new StringReader(CSV));
System.out.println("CsvDozerBeanReader with second CSV input:");
readWithCsvDozerBeanReader(new StringReader(CSV2));
}
private static void readWithCsvDozerBeanReader(final Reader reader) throws IOException {
ICsvDozerBeanReader beanReader = null;
try {
beanReader = new CsvDozerBeanReader(reader, PREFS);
final String[] header = beanReader.getHeader(true);
final String[] fieldMapping = new String[header.length];
final CellProcessor[] processors = new CellProcessor[header.length];
final Class<?>[] hintTypes = new Class<?>[header.length];
for( int i = 0; i < header.length; i++ ) {
if( i < ATT_START_INDEX ) {
fieldMapping[i] = header[i];
processors[i] = new NotNull();
} else {
fieldMapping[i] = String.format("attribs[%d]", i - ATT_START_INDEX);
processors[i] = new Optional(new ParsePersonAttribute(header));
hintTypes[i] = PersonAttribute.class;
}
}
beanReader.configureBeanMapping(Person.class, fieldMapping, hintTypes);
Person person;
while( (person = beanReader.read(Person.class, processors)) != null ) {
System.out.println(String.format("lineNo=%s, rowNo=%s, person=%s",
beanReader.getLineNumber(),
beanReader.getRowNumber(), person));
}
}
finally {
if( beanReader != null ) {
beanReader.close();
}
}
}
}
:
CsvDozerBeanReader with first CSV input:
lineNo=2, rowNo=2, person=Person [firstname=bill, lastname=smith, attribs=[PersonAttribute [key=dog_name, value=fido], PersonAttribute [key=fav_hat, value=porkpie], PersonAttribute [key=fav_color, value=blue]]]
lineNo=3, rowNo=3, person=Person [firstname=james, lastname=smith, attribs=[PersonAttribute [key=dog_name, value=rover], PersonAttribute [key=fav_hat, value=bowler], PersonAttribute [key=fav_color, value=purple]]]
CsvDozerBeanReader with second CSV input:
lineNo=2, rowNo=2, person=Person [firstname=tom, lastname=collins, attribs=[PersonAttribute [key=car_type, value=ford], PersonAttribute [key=floor_number, value=14]]]
lineNo=3, rowNo=3, person=Person [firstname=jim, lastname=jones, attribs=[PersonAttribute [key=car_type, value=toyota], PersonAttribute [key=floor_number, value=120]]]
, CsvDozerBeanReader Super CSV 2.0.1, ( , , / ) , :
"firstname","lastname","attribs[0]","attribs[1]"
Super CSV 2.1.0, . , Dozer , . 100%, PersonAttribute , () :
"firstname","lastname","attribs[0].value","attribs[1].value"
, :)