[ 
https://issues.apache.org/jira/browse/CSV-218?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16282053#comment-16282053
 ] 

Gary Gregory commented on CSV-218:
----------------------------------

I am not sure that you can tell when the next record is located if the current 
record is malformed. It may be the case in this example that you can tell where 
the next record is, but a general solution would be hard. Feel free to provide 
a patch and let's see what others think.

> CSVParser's iterator's hasNext throws IOException
> -------------------------------------------------
>
>                 Key: CSV-218
>                 URL: https://issues.apache.org/jira/browse/CSV-218
>             Project: Commons CSV
>          Issue Type: Bug
>          Components: Parser
>    Affects Versions: 1.5
>            Reporter: Max van den Aker
>            Priority: Minor
>             Fix For: Patch Needed, Discussion
>
>
> 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.)
> {code:java}
> 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");
>     }
> }
> {code}
> CSV sample, with entry no. 3 malformed (malformed.csv):
> id,firstName
> 1,"Ahmed"
> 2,"Sara"
> 3,"Ali
> 4,"Sama"
> 5,"Khaled"
> 6,"Ghada"



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to