I wrote a simple parser using JCSV to parse a csv content, but it seems that it doesn't maintain the end of line character inside enclosed single quotes, is there more configuration in it or JCSV is not capable of it?
public class CSVUtil {
final static Logger logger = LoggerFactory.getLogger(CSVUtil.class);
public static List<String[]> readCSV(InputStream is) throws IOException {
Reader reader = new InputStreamReader(is);
CSVReader<String[]> csvParser =
new CSVReaderBuilder<String[]>(reader)
.entryParser(new DefaultCSVEntryParser())
.strategy(CSVStrategy.UK_DEFAULT).build();
return csvParser.readAll();
}
public static List<String[]> readCSV(String csvdata) throws IOException {
InputStream is = new ByteArrayInputStream( csvdata.getBytes( "utf-8" ) );
return readCSV(is);
}
}
//my groovy test
@Test
public void testEndOfLineEnclosedWithSingleQuote() {
def csv = '''
one,'two 2 , 2 ,
2 , 2 2 2',three
one,two,three,four
'''
def results = CSVUtil.readCSV(csv);
println(results)
assert results.size == 2
}
Assertion failed:
assert results.size == 2
| | |
| 3 false
[[one, 'two 2 , 2 , ], [2 , 2 2 2', three], [one, two, three, four]]