问题
I'm reading csv files using superCSV reader and got the following exception. the file has 80000 lines. As I remove the end lines the exception still happens so there's some line in file that's causing this problem. how do I fix this?
org.supercsv.exception.SuperCsvException: unexpected end of file while reading quoted column beginning on line 80000 and ending on line 80000 context=null at org.supercsv.io.Tokenizer.readColumns(Tokenizer.java:198) at org.supercsv.io.AbstractCsvReader.readRow(AbstractCsvReader.java:179) at org.supercsv.io.CsvListReader.read(CsvListReader.java:69) at csv.filter.CSVFilter.filterFile(CSVFilter.java:400) at csv.filter.CSVFilter.filter(CSVFilter.java:369) at csv.filter.CSVFilter.main(CSVFilter.java:292)
ICsvListReader reader = null; String[] line=null; ListlineList=null; try{ reader = new CsvListReader(new FileReader(inputFile), CsvPreference.STANDARD_PREFERENCE); while((lineList=reader.read())!=null){ line=lineList.toArray(new String[lineList.size()]); } }catch(Exception exp){ exp.printStackTrace(); error=true; }
回答1:
The fact that the exception states it begins and ends on line 80000 should mean that there's an incorrect number of quotes on that line.
You should get the same error with the following CSV (but the exception will say line 1):
one,two,"three,four
Because the 3rd column is missing the trailing quote, so Super CSV will reach the end of the file and not know how to interpret the input.
FYI here is the relevant unit test for this scenario from the project source.
You can try removing lines to find the culprit, just remember that CSV can span multiple lines so make sure you remove whole records.
回答2:
The line shown in the error message is not necessarily the one with the problem, since unbalanced quotechars throw off SuperCSV's line detection.
If possible, open the csv in a spreadsheet problem (for instance libreoffice calc) and search (as in CTRL-F search) for the quote char.
Calc will usually import the file well, even if there is a mismatch but you will see the quotechar somewhere if you search for it. Then check in the csv if it is properly escaped. If it is, make sure SuperCSV knows about it. If it isn't, complain to the producer of the csv.
来源:https://stackoverflow.com/questions/25454737/org-supercsv-exception-supercsvexception-unexpected-end-of-file-while-reading-q