【问题标题】:Is there a way to skip the header when using CsvBeanReader?使用 CsvBeanReader 时有没有办法跳过标题?
【发布时间】:2020-04-02 08:25:19
【问题描述】:

我有一个项目,我应该在其中将对象写入 CSV 文件。我目前正在使用 ICsvBeanWriter ,但每次传递一条新记录时,它也会写入标题。从文件中读取时会产生问题。

以下分别是读写方法:

    public static ArrayList<communication> readCSV() throws IOException {
    ArrayList<communication> fileText = new ArrayList<>();
    ICsvBeanReader beanReader = new CsvBeanReader(new FileReader("products.csv"), CsvPreference.STANDARD_PREFERENCE);
    String[] header = beanReader.getHeader(true);
    CellProcessor[] processors = new CellProcessor[]{
            new ParseDouble(), // Distance
            new ParseDouble(), // Efficiency
            new ParseDouble(),// fuel
            new ParseDouble(),// total
    };
    communication com;
        while ((com = beanReader.read(communication.class, header, processors)) != null) {
                fileText.addAll(Collections.singletonList(com));
        }
    return fileText;
}

public static void writeCSV(double tripDistance, double fuelEfficiency, double costOfFuel, double totalCost) throws Exception {

    // create a list of employee
    List<communication> EmployeeList = new ArrayList<>();
    EmployeeList.add(new communication(tripDistance, fuelEfficiency, costOfFuel, (Math.round(totalCost * 100.0) / 100.0)));

    ICsvBeanWriter beanWriter = new CsvBeanWriter(new FileWriter("products.csv",true),
            CsvPreference.STANDARD_PREFERENCE);

    String[] header = new String[]{"TripDistance", "FuelEfficiency", "FuelCost", "Total"};

    beanWriter.writeHeader(header);
    CellProcessor[] processors = new CellProcessor[]{
            new ParseDouble(), // Distance
            new ParseDouble(), // Efficiency
            new ParseDouble(),// fuel
            new ParseDouble(),// total

    };

    for (communication com : EmployeeList) {
        beanWriter.write(com, header, processors);
    }
    beanWriter.close();
}

我想要一种跳过写入或读取标题的方法,或者创建删除所有标题行的方法(跳过第一行)。

这是出现的错误:

org.supercsv.exception.SuperCsvCellProcessorException: 'TripDistance' could not be parsed as a Double
processor=org.supercsv.cellprocessor.ParseDouble

【问题讨论】:

    标签: java supercsv


    【解决方案1】:

    这些代码行创建了一个标题:

    String[] header = new String[]{"TripDistance", "FuelEfficiency", "FuelCost", "Total"};
    beanWriter.writeHeader(header);
    

    我想你可以从你的代码中删除它们。

    【讨论】:

    • 而不是删除标头初始化部分删除writeHeader()工作
    【解决方案2】:

    首先,您使用 opencsv 标记了它,但您似乎实际上是根据代码中的类名使用 supercsv。此答案基于该观察。

    阅读时跳过标题的两种方式。

    选项 1:阅读器类有一个 getHeader() 方法,根据文档可以完成这项工作。

    https://super-csv.github.io/super-csv/apidocs/org/supercsv/io/AbstractCsvReader.html#getHeader-boolean-

    选项 2:传递给 CsvBeanReader 的第一个参数是一个应该传递数据的读取器。

    所以如果你想跳过一个标题,你可以把它传递给一个已经这样做的阅读器。 BufferedReader 具有读取一行的便捷方式,因此您可以丢弃第一行。这是假设 CSV 格式正确且顶部没有空行。

    BufferedReader br = new BufferedReader(new FileReader("products.csv"));
    br.readLine(); // discard header
    
    ICsvBeanReader beanReader = new CsvBeanReader(br, CsvPreference.STANDARD_PREFERENCE);
    

    【讨论】:

    • 如果我选择选项 2,它只会跳过第一个标题而不是其余部分。 @金比
    • 这是一个很奇怪的说法。应该只有第一个标题。 “其余的”是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2015-08-09
    • 1970-01-01
    相关资源
    最近更新 更多