Exception in thread "main" org.supercsv.exception.SuperCsvException: The number of columns to be processed (229326) must match the number of CellProcessors (8):
我相信我可能不得不重做我使用 supercsv 所做的事情,因为从长远来看它可能会更容易,但我对任何其他建议持开放态度。我只是想写回一个 csv 文件,我有一个包含所有数据的列表,但是输出是这样的
4350 02/06/2013 3965.21 0.0 0.0 0.0 0.0 0.0,
4698 02/06/2013 498.16 0.0 0.0 0.0 0.0 0.0,
4992 02/06/2013 97.87 87.82 6.05 0.0 0.0 0.0,
4441 02/06/2013 18.8 71.98 11.6 0.0 0.0 -42.5, 54092 02/06/2013 105.11 118.82 6.24 0.0 0.0 0.0,
我已经设法通过替换列表中的字符串来获得我想要的输出,但是当它运行时它会挂起,我相信这是由于我如何写回 csv,我不确定,还有什么可以做的而不是将其写回 csv 不同地不使用超级 csv。我得到的错误是
"1805","31/07/2013","-233.4","0.0","0.0","0.0","0.0","0.0"
"8054","31/07/2013","280.45","82.38","52.38","0.0","0.0","-200.0"The number of columns to be processed (1) must match the number of CellProcessors (8):
我的witer类如下
package writer;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.supercsv.cellprocessor.FmtDate;
import org.supercsv.cellprocessor.ParseDouble;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvListWriter;
import org.supercsv.io.ICsvListWriter;
import org.supercsv.prefs.CsvPreference;
public class RentStatementsWriter {
public ArrayList rData;
private List<String> csvData;
char b = ',';
public RentStatementsWriter(ArrayList rentData) {
rData = rentData;
ICsvListWriter listWriter = null;
try{
listWriter = new CsvListWriter(new FileWriter("rentl.csv"),CsvPreference.STANDARD_PREFERENCE);
CellProcessor[] processors =new CellProcessor[]{
new ParseInt(),
new FmtDate("dd/MM/yyyy"),//
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
};
final String [] header = new String []{"_num", "End_Date", "bal_val","rval","cval","bf_val","aval","pval"};
System.out.print("to string "+rData.toString().replaceFirst(" ", "\"").replaceAll("\\,"," \\,").replaceAll(" ", "").replaceAll(" ", "\"\\,\"").replaceAll("\"\\,\"\\,", "\"\n\""));
csvData = Arrays.asList(rData.toString().replaceFirst(" ", "\"").replaceAll("\\,"," \\,").replaceAll(" ", "").replaceAll(" ", "\"\\,\"").replaceAll("\"\\,\"\\,", "\"\""));
/*
* replace
* .replaceAll(" ", "\"\\,\"")
*/
listWriter.writeHeader(header);
listWriter.write(csvData, processors);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(e+" file unable to write");
} finally {
if(listWriter !=null){
try {
listWriter.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("list writer");
}
}
}
}
String listToCsv(List<String> listOfStrings, char separator) {
StringBuilder sb = new StringBuilder();
// all but last
for(int i = 0; i < listOfStrings.size() - 1 ; i++) {
sb.append("\""+listOfStrings.get(i)+"\"");
sb.append(separator);
}
// last string, no separator
sb.append(listOfStrings.get(listOfStrings.size()-1));
return sb.toString();
}
}
我在这个语法中缺少什么,或者有没有更好的方法来完成这个任务