解析 java 中的 CSV 文件,并处理空值
Parse CSV file in java, and delaing with empty values
我正在将一个 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
。
如果我知道文件中的对象数量将保持不变,我该如何最好地处理这个问题?
有人告诉我需要用空值替换空值。
来自 String.split(regex)
的 Javadoc
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
因此,在您的情况下,当字符串以 ,,
结尾时,空字符串将不会成为结果数组的一部分。
修复:使用这种拆分变体
line.split(DELIMITER, -1);
这将包括所有尾随的空字符串。所以你不会得到例外。
在尝试使用最后一个(在这种情况下不存在的)值之前检查数组的长度
myArray.length
如果列为空,则此代码生成的数组元素为空。
// ... rest of OP's code
public String[] nextLine() throws IOException
{
final String line = br.readLine();
if(line == null)
{
return null;
}
String columns[] = line.split(DELIMITER, -1);
for(int i = 0; i < columns.length; i++)
{
if(columns[i].isEmpty())
{
columns[i] = null;
}
}
return columns;
}
你也可以这样。只需在以逗号
返回的字符串上调用 split 方法
public String replaceNullSplitLine(String line){
if(line.endsWith(",")){
line = line+"***";
}
line = line.replaceAll(",,", ",***,");
return line;
}
我正在将一个 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
。
如果我知道文件中的对象数量将保持不变,我该如何最好地处理这个问题?
有人告诉我需要用空值替换空值。
来自 String.split(regex)
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
因此,在您的情况下,当字符串以 ,,
结尾时,空字符串将不会成为结果数组的一部分。
修复:使用这种拆分变体
line.split(DELIMITER, -1);
这将包括所有尾随的空字符串。所以你不会得到例外。
在尝试使用最后一个(在这种情况下不存在的)值之前检查数组的长度
myArray.length
如果列为空,则此代码生成的数组元素为空。
// ... rest of OP's code
public String[] nextLine() throws IOException
{
final String line = br.readLine();
if(line == null)
{
return null;
}
String columns[] = line.split(DELIMITER, -1);
for(int i = 0; i < columns.length; i++)
{
if(columns[i].isEmpty())
{
columns[i] = null;
}
}
return columns;
}
你也可以这样。只需在以逗号
返回的字符串上调用 split 方法public String replaceNullSplitLine(String line){
if(line.endsWith(",")){
line = line+"***";
}
line = line.replaceAll(",,", ",***,");
return line;
}