Uploaded image for project: 'Commons CSV'
  1. Commons CSV
  2. CSV-218

CSVParser's iterator's hasNext throws IOException

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.5
    • Fix Version/s: Patch Needed, Discussion
    • Component/s: Parser
    • Labels:
      None

      Description

      Method hasNext() of the CSVParser iterator throws an IOException if the next entry in the CSV file contains a malformed record. IMHO, it's preferable that hasNext() merely indicates whether or not a next entry is available. (Method next() will then throw the exception if the record is malformed. This way it's easier to continue parsing after encountering a malformed record.)

      import java.io.FileReader;
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Iterator;
      import org.apache.commons.csv.CSVFormat;
      import org.apache.commons.csv.CSVParser;
      import org.apache.commons.csv.CSVRecord;
      
      public class CSVFileReader {
          private static final String [] FILE_HEADER_MAPPING = {"id","firstName"};
          private static final String STUDENT_ID = "id";
          private static final String STUDENT_FNAME = "firstName";
          
          public static void read(String fileName) {
              FileReader fileReader = null;
              CSVParser csvFileParser = null;
              
              CSVFormat csvFileFormat = CSVFormat.RFC4180.
                      withDelimiter(',').
                      withQuote('"').
                      withRecordSeparator("\r\n").
                      withIgnoreEmptyLines(true).
                      withAllowMissingColumnNames(false).
                      withFirstRecordAsHeader();
      
              try {
                  fileReader = new FileReader(fileName);
                  csvFileParser = csvFileFormat.parse(fileReader);
              } catch (Exception e) { 
                  e.printStackTrace();
              }
      
              Iterator<CSVRecord> iter = csvFileParser.iterator();
      
              try {
                  while(iter.hasNext()) {
                      try {
                          CSVRecord record = iter.next();
                          System.out.println(record.get(STUDENT_ID) + " " + record.get(STUDENT_FNAME));
                      } catch (Exception e) {
                          //e.printStackTrace();
                      }
                  }
              } catch (Exception e) {
                  System.out.println("iter.hasNext() exception");
              }
      
              try {
                  fileReader.close();
                  csvFileParser.close();
              } catch (Exception e) {
                  //e.printStackTrace();
              }
          }
      
          public static void main(String[] args) {
              CSVFileReader.read("./malformed.csv");
          }
      
      }
      

      CSV sample, with entry no. 3 malformed (malformed.csv):

      id,firstName
      1,"Ahmed"
      2,"Sara"
      3,"Ali
      4,"Sama"
      5,"Khaled"
      6,"Ghada"

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mvdasecmatters Max van den Aker
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: