|
18 | 18 | import com.opencsv.CSVParserBuilder;
|
19 | 19 | import com.opencsv.CSVReader;
|
20 | 20 | import com.opencsv.CSVReaderBuilder;
|
21 |
| -import com.opencsv.exceptions.CsvValidationException; |
22 |
| -import com.opencsv.validators.LineValidator; |
23 | 21 | import org.apache.commons.lang3.StringUtils;
|
24 | 22 | import org.apache.commons.lang3.Validate;
|
25 | 23 |
|
| 24 | +import java.io.IOException; |
26 | 25 | import java.io.InputStream;
|
27 | 26 | import java.io.InputStreamReader;
|
28 | 27 | import java.io.Reader;
|
@@ -55,15 +54,20 @@ public CsvReader(Reader openCsvReader) {
|
55 | 54 | public CsvReader(Reader openCsvReader, char delimiter) {
|
56 | 55 | this.openCsvReader = new CSVReaderBuilder(openCsvReader)
|
57 | 56 | .withCSVParser(new CSVParserBuilder().withSeparator(delimiter).build())
|
58 |
| - .withLineValidator(new IgnoreEmptyLines()) |
59 | 57 | .build();
|
60 | 58 | }
|
61 | 59 |
|
62 | 60 | public boolean readRecord() {
|
63 | 61 | try {
|
64 |
| - currenRecord = this.openCsvReader.readNext(); |
65 |
| - return currenRecord != null; |
66 |
| - } catch (Exception e) { |
| 62 | + do { |
| 63 | + currenRecord = this.openCsvReader.readNextSilently(); |
| 64 | + if (currenRecord == null) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } while (currenRecord.length == 1 && StringUtils.isEmpty(currenRecord[0])); |
| 68 | + return true; |
| 69 | + } catch (IOException e) { |
| 70 | + currenRecord = null; |
67 | 71 | return false;
|
68 | 72 | }
|
69 | 73 | }
|
@@ -91,19 +95,4 @@ public String get(String column) {
|
91 | 95 | ? currenRecord[index]
|
92 | 96 | : "";
|
93 | 97 | }
|
94 |
| - |
95 |
| - private static class IgnoreEmptyLines implements LineValidator { |
96 |
| - |
97 |
| - @Override |
98 |
| - public boolean isValid(String s) { |
99 |
| - return StringUtils.isNotEmpty(s); |
100 |
| - } |
101 |
| - |
102 |
| - @Override |
103 |
| - public void validate(String s) throws CsvValidationException { |
104 |
| - if (!isValid(s)) { |
105 |
| - throw new CsvValidationException(); |
106 |
| - } |
107 |
| - } |
108 |
| - } |
109 | 98 | }
|
0 commit comments