将CSV文件解析到Castom对象,该文件具有诸如Enum之类的字段

发布于 2025-02-07 01:24:10 字数 1413 浏览 0 评论 0原文

问题是,我应该如何处理枚举字段以进行正确的解析。也许它有另一种注释或其他内容...我从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文