【发布时间】:2015-09-28 13:32:59
【问题描述】:
我有一个程序读取CSV 文件并将第一行和最后一行存储为JSONObject。我当前的实现是跳过最后一行并读入 null 值作为数据对象。
创建/保存 CSV 文件以消除任何空白或空值的正确方法是什么?
CSV
TIME_STAMP,TOTAL,APPLES,ORANGES,PEARS,GRAPES,TANGERINES,PINEAPPLES,CARROTS,UNKNOWN
7/1/2015 4:00,19474,1736,275,8366,5352,3003,393,349,
代码
String firstLine = "";
String lastLine = "";
int count = 0;
if(reader != null){
String aux = "";
String lastLineMinusOne = "";
while ((aux = reader.readLine()) != null) {
if(count == 0)firstLine = aux;
lastLineMinusOne = lastLine;
lastLine = aux;
count ++;
}
logger.info("Count = " + count);
String[] columns = firstLine.split(",");
String[] data = lastLine.split(",");
logger.info(firstLine);
logger.info(lastLine);
日志
2015-09-28 13:41:42,370 [ajp-0.0.0.0-8009-2] INFO com.ChartData - Count = 3
2015-09-28 13:23:27,745 [ajp-0.0.0.0-8009-3] INFO com.ChartData - TIME_STAMP,TOTAL,APPLES,ORANGES,PEARS,GRAPES,TANGERINES,PINEAPPLES,CARROTS,UNKNOWN
2015-09-28 13:23:27,745 [ajp-0.0.0.0-8009-3] INFO com.ChartData -
错误
java.lang.ArrayIndexOutOfBoundsException: 10
at com.ChartData.getCSV(ChartData.java:75)
第 75 行 -> jObject.put("val", data[i]);
【问题讨论】: