Reputation: 3
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
Upvotes: 0
Views: 636
Reputation: 5282
First of all, you tagged this with opencsv but you seem to actually be using supercsv based on the classnames in your code. This answer is based on that observation.
Two ways to skip the header when reading.
Option 1: the reader class has a getHeader() method which according to the documentation can do the job.
Option 2: The first parameter passed to the CsvBeanReader is a reader which is supposed to deliver the data.
So if you want to skip a header, you can pass it a reader which has already done that. A BufferedReader has a convenient way of reading a line so you can discard the first one. This is assuming the CSV is properly formatted with no empty lines at the top.
BufferedReader br = new BufferedReader(new FileReader("products.csv"));
br.readLine(); // discard header
ICsvBeanReader beanReader = new CsvBeanReader(br, CsvPreference.STANDARD_PREFERENCE);
Upvotes: 0
Reputation: 46
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.
Upvotes: 1