You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Max van den Aker (JIRA)" <ji...@apache.org> on 2017/12/07 15:36:00 UTC
[jira] [Updated] (CSV-218) CSVParser's iterator's hasNext throws
IOException
[ https://issues.apache.org/jira/browse/CSV-218?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Max van den Aker updated CSV-218:
---------------------------------
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.)
{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"
was:
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","lastName","gender","age"};
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"
> 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)