gpt4 book ai didi

java - 使用 CsvBeanReader 时有没有办法跳过标题?

转载 作者:行者123 更新时间:2023-12-02 08:47:25 25 4
gpt4 key购买 nike

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

下面分别是读取和写入的方法:

    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

最佳答案

这些代码行创建一个 header :

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

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

关于java - 使用 CsvBeanReader 时有没有办法跳过标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60987176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com