[ https://issues.apache.org/jira/browse/CSV-218?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17424452#comment-17424452 ]
Rick Oosterholt commented on CSV-218: ------------------------------------- I'm running into a similar issue; when an invalid character is located inside a record, the same thing happens. Imagine: id,firstName 1,"Ahmed" 2,"Sara" 3,"Ali"" 4,"Sama" 5,"Khaled" 6,"Ghada" so an unescaped quote on line 3 (correct line would be: 3,"Ali""") I'm also searching for a solution to only skip line 3 in cases like this and continue to the next. Did you find a solution already? > 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 (v8.3.4#803005)