Silver Quettier
Silver Quettier

Reputation: 2050

How to "disable" comments detection when parsing a file with CSVParser / CSVStrategy (Apache Commons)

In a Java program, I need to parse a CSV file. Nothing extremely unusual there. The implementation I use is Apache Commons's CSVParser, witch now uses a CSVStrategy as parameter to define the CSV dialect.

The thing is, I need to get rid of the option to look for comments in the input file. The input file is supposed to have no comments, and every character (except ;, of course) should be kept and be considered meaningful. Especially, the standard # character should not be a comment.

Here is the code I use so far (which cause problems with #, as expected) :

fRead = new FileReader(someFilePath);
data = new CSVParser(fRead, new CSVStrategy(';', '"', '#')).getAllValues();

The thrid argument of CSVStrategy's constructor is a char, so it cannot be null.

Something interesting is the presence of the static char COMMENTS_DISABLED and method isCommentingDisabled() in CSVStrategy, yet no setter for something similar.

There is a constructor of CSVParser which takes only the reader and a delimiter char as parameters, and no CSVStrategy, but it's deprecated. So I figure there is a way to achieve similar functionality with the CSVStrategy ?

Upvotes: 1

Views: 1254

Answers (1)

ndeverge
ndeverge

Reputation: 21564

By quickly looking at the source code, it seems that you can use the CSVStrategy constructor like this :

data = new CSVParser(fRead, new CSVStrategy(';', '"', CSVStrategy.COMMENTS_DISABLED)).getAllValues();

Upvotes: 2

Related Questions