将CSV文件解析到Castom对象,该文件具有诸如Enum之类的字段
问题是,我应该如何处理枚举字段以进行正确的解析。也许它有另一种注释或其他内容...我从CSV文件中获得符号,并且必须以正确的类型创建枚举。但是我该怎么做?...
这个实现传达了太长的消息。第一个字符串是:
线程“ main” java.lang.runtimeException中的例外:com.opencsv.exceptions.csvbeanintrospectionsception:确定给定豆类类型的基本实例(以及通过递归创建的下属豆(如果适用)确定为不可能是不可能的。
它将其扔进字符串中,并使用[.parse();]
公共类FileServiceImplementationsblincementsfileservice {
public List<FruitTransaction> read(File file) {
try (FileReader reader = new FileReader(file)) {
return new CsvToBeanBuilder(reader)
.withType(FruitTransaction.class)
.build()
.parse();
} catch (IOException e) {
throw new RuntimeException("Cannot read file: " + file.getName() + e);
}
}
}
public class FruitTransaction {
@CsvBindByPosition(position = 0)
private Operation operation;
@CsvBindByPosition(position = 1)
private String fruit;
@CsvBindByPosition(position = 2)
private int quantity;
...
// and my nested enum class:
public enum Operation {
BALANCE("b"),
SUPPLY("s"),
PURCHASE("p"),
RETURN("r");
private String operation;
Operation(String operation) {
this.operation = operation;
}
}
}
my input data from CSV file:
b,banana,20
b,apple,100
s,banana,100
p,banana,13
r,apple,10
p,apple,20
p,banana,5
s,banana,50
The question is, how I should handle ENUM fields for right parsing. Maybe it has another sort of annotation or something... I get symbol from csv file and have to create the enum in right type. But how can I do that?...
This implementation throw too long message. The first string is:
Exception in thread "main" java.lang.RuntimeException: com.opencsv.exceptions.CsvBeanIntrospectionException: Basic instantiation of the given bean type (and subordinate beans created through recursion, if applicable) was determined to be impossible.
It throws it in a string with [.parse();]
public class FileServiceImplementation implements FileService {
public List<FruitTransaction> read(File file) {
try (FileReader reader = new FileReader(file)) {
return new CsvToBeanBuilder(reader)
.withType(FruitTransaction.class)
.build()
.parse();
} catch (IOException e) {
throw new RuntimeException("Cannot read file: " + file.getName() + e);
}
}
}
public class FruitTransaction {
@CsvBindByPosition(position = 0)
private Operation operation;
@CsvBindByPosition(position = 1)
private String fruit;
@CsvBindByPosition(position = 2)
private int quantity;
...
// and my nested enum class:
public enum Operation {
BALANCE("b"),
SUPPLY("s"),
PURCHASE("p"),
RETURN("r");
private String operation;
Operation(String operation) {
this.operation = operation;
}
}
}
my input data from CSV file:
b,banana,20
b,apple,100
s,banana,100
p,banana,13
r,apple,10
p,apple,20
p,banana,5
s,banana,50
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论