1

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]]
Teemu
  • 22,918
  • 7
  • 53
  • 106
nightograph
  • 2,179
  • 5
  • 31
  • 49
  • 1
    CSV is not exactly a standard, but as far as the [RFC-4180](http://tools.ietf.org/html/rfc4180) goes, the quotes must be double, not single. – RealSkeptic Mar 26 '15 at 18:39
  • @RealSkeptic thanks for pointing that out, enclosing in double quotes work. I can deal with that! – nightograph Mar 26 '15 at 18:46

0 Answers0