Parse CSV file in java, and delaing with empty values

早过忘川 提交于 2019-12-13 12:30:35

问题


I am parsing a CSV file into my program, spliting the values at the , element, and it's working fine, except for when I have lines that have missing values.

The parser is working like this:

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);
    }
}

The lines of data look like this (one line as an example):

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,,

Most of the lines in the file complete with this: 50,85028197242127,8640

But on some lines, the data is missing, so it ends like this: 50,,

When the file is being processed, these lines are causing a java.lang.ArrayIndexOutOfBoundsException.

How can I best deal with this, if I know that the numbers of objects in the file will remain constant?

I've been told that I need to replace empty values with a null value.


回答1:


From the Javadoc of 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.

So, in your case, when the string ends with ,,, empty strings wont be part of the resultant array.

To Fix: Use this variant of split

line.split(DELIMITER, -1);

This will include all trailing empty strings. So you won't get an exception.




回答2:


This code results in array elements that are null if the column was empty.

// ... 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;
}



回答3:


You can this way also. Just call split method on the String returned with comma

public String replaceNullSplitLine(String line){
    if(line.endsWith(",")){
        line = line+"***";
    }
    line = line.replaceAll(",,", ",***,");
    return line;
}



回答4:


Check the length of the array before trying to use the last (and in that case non-existed) values

myArray.length



来源:https://stackoverflow.com/questions/32068129/parse-csv-file-in-java-and-delaing-with-empty-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!