In my case, valid CSVs are those that are limited to a comma or semicolon. I am open to other libraries, but it should be Java. Reading through the Apache CSVParser API, the only thing I can think of is to do this, which seems inefficient and ugly.
try { BufferedReader reader = new BufferedReader(new InputStreamReader(file)); CSVFormat csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(';'); CSVParser parser = csvFormat.parse( reader ); // now read the records } catch (IOException eee) { try { // try the other valid delimeter csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(','); parser = csvFormat.parse( reader ); // now read the records } catch (IOException eee) { // then its really not a valid CSV file } }
Is there a way to check the delimiter first, or perhaps allow two delimiters? Does anyone have an idea better than just catching an exception?
java csv apache-commons-csv
Coder1224
source share