1

if a CSV file contains three columns and if the values are as given below

a,b,c
     //empty line
,,,
a,b,c

There are two valid records. Using Apache commons CSV parser, i could easily skip the record which has empty lines. But when the records contain only null values, how to skip it then?

To overcome this, I'm using String equals() with already constructed empty record. Here is a sample implementation.

List<String[]> csvContentsList = new ArrayList<String[]>();
CSVFormat csvFormat = CSVFormat.DEFAULT.withNullString("");
CSVParser csvParser = new CSVParser(fileReader, csvFormat);

String[] nullRecordArray = { null, null, null};
String nullRecordString = Arrays.toString(nullRecordArray);
for (CSVRecord csvRecord : csvParser) {
    try {
        String values[] = { csvRecord.get(0),csvRecord.get(1),csvRecord.get(2) };
        if (!nullRecordString.equals(Arrays.toString(values))) //lineA
            csvContentsList.add(values);
    } catch (Exception e) {
        // exception handling
    }
}

When i don't use the line marked as 'lineA', this implementation gives three records in the csvContentsList as below

[a,b,c]
[null,null,null]
[a,b,c]

Is there any inbuilt way to do this? or any other better way?

2 Answers 2

2

Find here another possible solution.

CSVFormat csvFormat = CSVFormat.DEFAULT.withNullString("");
CSVParser csvParser = new CSVParser(fileReader, csvFormat);
for (CSVRecord csvRecord : csvParser.getRecords()) {
    String values[] = {csvRecord.get(0), csvRecord.get(1), csvRecord.get(2)};
    for (String value : values) {
        if (value != null) {
            // as soon a value is not-null we add the array
            // and exit the for-loop
            csvContentsList.add(values);
            break;
        }
    }
}

assumend input

a,b,c

,,,
d,e,f

output

a,b,c
d,e,f

edit If you can use Java 8 a solution might be.

List<String[]> csvContentsList = csvParser.getRecords()
        .stream() 
        .sequential() // 1.
        .map((csvRecord) -> new String[]{
            csvRecord.get(0), 
            csvRecord.get(1), 
            csvRecord.get(2)
        }) // 2.
        .filter(v -> Arrays.stream(v)
                .filter(t -> t != null)
                .findFirst()
                .isPresent()
        ) // 3.
        .collect(Collectors.toList()); // 4.
  1. if the order of lines is important
  2. map a csvRecord to a String[]
  3. filter on String arrays with at least one non-null value
  4. collect all values and return a List

Might need to be amended, depending on your requirements.

Sign up to request clarification or add additional context in comments.

2 Comments

CSVFormat.Default already contains withIgnoreEmptyLines(true). commons.apache.org/proper/commons-csv/apidocs/org/apache/…. And in the question, i have already mentioned that i am able to skip empty lines.
@Ram I added a Java 8 example using the Stream API.
1

You could try StringUtils#isNotBlank() this way:

if (StringUtils.isNotBlank(csvRecord.get(0)) 
     && StringUtils.isNotBlank(csvRecord.get(1)) 
     && StringUtils.isNotBlank(csvRecord.get(2))) {        
   csvContentsList.add(values); 
}

2 Comments

From where to get this StringUtils class? It is not part Apache commons CSV.
@Ram visit this site to download common lang jar commons.apache.org/proper/commons-lang/download_lang.cgi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.