我正在尝试使用univocity解析器将csv文件解析为bean的新实例和现有实例。csv是通过使用univocity为一组bean生成的,我将其称为set A。
现在,我想要重新阅读csv,执行以下操作:
案例1:如果行对应于集合A中最初存在的bean,我不想创建新的bean实例,而是将csv读取到“现有”实例中。(即“更新”实例)。我使用bean的UUID检查是否存在。
案例2:如果该行不对应于集合A中最初存在的bean,我想为它创建一个新的bean实例。
我想解决的问题:对于第一种情况,我如何写入现有的bean实例?
在supercsv中,我可以这样做:
beanReader.read(targetExistingBean, header, processors);我如何才能在univocity中做到这一点?
发布于 2017-07-09 11:12:13
目前,唯一的方法是覆盖BeanProcessor或BeanListProcessor (无论您使用哪个)的createBean方法:
BeanListProcessor<Car> carProcessor = new BeanListProcessor<Car>(Car.class){
@Override
public Car createBean(String[] row, Context context) {
//analyze the row here to determine whether to return an existing instance
//if you need a new instance, call super.createBean(row, context);
}
};希望这能有所帮助。
https://stackoverflow.com/questions/44954711
复制相似问题