올 :
나는이 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);
나는 당신이 당신의 코드에서 제거 할 수 있습니다 같아요.