本文整理汇总了Java中com.univocity.parsers.common.processor.BeanWriterProcessor类的典型用法代码示例。如果您正苦于以下问题:Java BeanWriterProcessor类的具体用法?Java BeanWriterProcessor怎么用?Java BeanWriterProcessor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BeanWriterProcessor类属于com.univocity.parsers.common.processor包,在下文中一共展示了BeanWriterProcessor类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toFile
import com.univocity.parsers.common.processor.BeanWriterProcessor; //导入依赖的package包/类
/**
* 将数据库的数据导出为CSV
*
* @param file 导出的文件
* @param sourceClass 读取数据库数据时对应的Bean
* @param targetClass 写入文件时对应的Bean
* @param sourceList 数据库查到的数据
*/
@SneakyThrows(IOException.class)
public static <F, T> void toFile(Path file, Class<F> sourceClass, Class<T> targetClass, List<F> sourceList) {
if (Files.notExists(file)) {
Files.createFile(file);
}
Copier<F, T> copier = Copiers.create(sourceClass, targetClass);
List<T> data = sourceList.parallelStream().map(copier::copy).collect(Collectors.toList());
//List转换CSV处理器
BeanWriterProcessor<T> writerProcessor = new BeanWriterProcessor<>(targetClass);
writerProcessor.setStrictHeaderValidationEnabled(true);
//CSV写入设置
CsvWriterSettings settings = Csv.writeExcel();
//设置处理器
settings.setRowWriterProcessor(writerProcessor);
try (OutputStream os = Files.newOutputStream(file, CREATE, TRUNCATE_EXISTING, WRITE);
OutputStreamWriter osw = new OutputStreamWriter(os)) {
CsvWriter writer = new CsvWriter(osw, settings);
writer.processRecordsAndClose(data);
}
}
示例2: setUp
import com.univocity.parsers.common.processor.BeanWriterProcessor; //导入依赖的package包/类
@Setup(Level.Iteration)
public void setUp() {
dsl = CsvWriter.from(SmallBenchmarkObject.class).skipHeaders();
sbo = new SmallBenchmarkObject();
sbo.setId(2);
sbo.setEmail("[email protected]");
sbo.setName("name");
sbo.setYearStarted(222);
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(SmallBenchmarkObject.class);
myObjectWriter = mapper.writer(schema);
settings = new CsvWriterSettings();
settings.setHeaders("id", "year_started", "name", "email");
settings.setRowWriterProcessor(new BeanWriterProcessor<SmallBenchmarkObject>(SmallBenchmarkObject.class));
}