Merge pull request #5272 from eugenp/open-csv-ms
add mappingstrategy opencsv method
This commit is contained in:
commit
dbcbd1fde0
|
@ -18,16 +18,19 @@ import java.util.List;
|
||||||
public class BeanExamples {
|
public class BeanExamples {
|
||||||
|
|
||||||
public static List<CsvBean> beanBuilderExample(Path path, Class clazz) {
|
public static List<CsvBean> beanBuilderExample(Path path, Class clazz) {
|
||||||
|
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
|
||||||
|
return beanBuilderExample(path, clazz, ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<CsvBean> beanBuilderExample(Path path, Class clazz, MappingStrategy ms) {
|
||||||
CsvTransfer csvTransfer = new CsvTransfer();
|
CsvTransfer csvTransfer = new CsvTransfer();
|
||||||
try {
|
try {
|
||||||
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
|
|
||||||
ms.setType(clazz);
|
ms.setType(clazz);
|
||||||
|
|
||||||
Reader reader = Files.newBufferedReader(path);
|
Reader reader = Files.newBufferedReader(path);
|
||||||
CsvToBean cb = new CsvToBeanBuilder(reader)
|
CsvToBean cb = new CsvToBeanBuilder(reader).withType(clazz)
|
||||||
.withType(clazz)
|
.withMappingStrategy(ms)
|
||||||
.withMappingStrategy(ms)
|
.build();
|
||||||
.build();
|
|
||||||
|
|
||||||
csvTransfer.setCsvList(cb.parse());
|
csvTransfer.setCsvList(cb.parse());
|
||||||
reader.close();
|
reader.close();
|
||||||
|
@ -40,11 +43,10 @@ public class BeanExamples {
|
||||||
|
|
||||||
public static String writeCsvFromBean(Path path) {
|
public static String writeCsvFromBean(Path path) {
|
||||||
try {
|
try {
|
||||||
Writer writer = new FileWriter(path.toString());
|
Writer writer = new FileWriter(path.toString());
|
||||||
|
|
||||||
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer)
|
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer).withSeparator(CSVWriter.DEFAULT_SEPARATOR)
|
||||||
.withSeparator(CSVWriter.DEFAULT_SEPARATOR)
|
.build();
|
||||||
.build();
|
|
||||||
|
|
||||||
List<CsvBean> list = new ArrayList<>();
|
List<CsvBean> list = new ArrayList<>();
|
||||||
list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd"));
|
list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd"));
|
||||||
|
|
Loading…
Reference in New Issue