来ます:
私は、私は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();
}
私はそれのいずれかのスキップは、ヘッダを書き込みまたは読み取りする方法を好むか、すべてのヘッダー行(1行目をスキップする)を削除するメソッドを作成します。
これが表示されますエラーです。
org.supercsv.exception.SuperCsvCellProcessorException: 'TripDistance' could not be parsed as a Double
processor=org.supercsv.cellprocessor.ParseDouble
マキシムIrinarkhov:
コードのこれらの行は、ヘッダを作成します。
String[] header = new String[]{"TripDistance", "FuelEfficiency", "FuelCost", "Total"};
beanWriter.writeHeader(header);
私はあなたがちょうどあなたのコードからそれらを削除することができますね。