我正在将CSV文件解析到我的程序中,将值拆分为,元素,并且它工作正常,除非我有缺少值的行.
解析器的工作方式如下:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CsvReader
{
private static final String DELIMITER = ",";
private final BufferedReader br;
private final String path;
public CsvReader(final String path) throws IOException
{
this.path = path;
this.br = new BufferedReader(new FileReader(path));
}
public String[] nextLine() throws IOException
{
final String line = br.readLine();
return (line == null) ? new String[0] : line.split(DELIMITER);
}
}
数据行看起来像这样(一行作为例子):
J1024205,5028197000004,1,,00,20150603,,Accessories,Factory Test Article (m),ENG,010,110,5,T1,99,99,,,99,99,99,ZZ,ZZ,,5028197242053,30,35028197242054,6,,,OPZ848,3013607800239,OPZ848,,,,50,,
该文件中的大多数行都包含:50,85028197242127,8640
但在某些方面,数据缺失,所以结尾如下:50 ,,
处理文件时,这些行会导致java.lang.ArrayIndexOutOfBoundsException.
如果我知道文件中的对象数量将保持不变,我怎么能最好地解决这个问题呢?
我被告知我需要用空值替换空值.