Is there a way to skip the header when using CsvBeanReader?

hatim :

I have a project in which I am supposed to write the object into a CSV file. I am currently using ICsvBeanWriter for it but each time a new record is passed it writes the header as well. It creates issues when reading from the file.

Below are methods for reading and writing respectively:

    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();
}

I would like a way in which it either skips writing or reading the header or create method that deletes all the header rows(skipping the 1st row).

This is the error that appears:

org.supercsv.exception.SuperCsvCellProcessorException: 'TripDistance' could not be parsed as a Double
processor=org.supercsv.cellprocessor.ParseDouble
Максим Иринархов :

These lines of code create a header:

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

I guess you can just remove them from you code.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=395930&siteId=1