I am writing a java program to write data to a csv file which fetches a key's count value from database and writes the count corresponding to each key in the file. I have accomplished it using FileWriter whose pseudocode goes like below
while (keys.hasNext()) {
writer.append(keys.next().getCount());
writer.append(',');
}
// where keys is the list of the keys
The headers are also appended in the above way. Now I have come across open source libraries such as OpenCSV and CommonsCSV for writing to csv files.
So now am wondering whether using the libraries is better or using the above mentioned way of writing to CSV file. Can someone please tell me which way is better in terms of readability and efficiency?
It's pretty much up to you. Here's the OpenCSV equivalent of your code:
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
...
String[] row = new String[];
int i=0;
while(keys.hasNext()) {
row[i++] = keys.next().getCount();
}
writer.writeNext(entries);
Is that more or less readable than yours? That's subjective and up to you. I can tell you that yours is not inefficient.
It's worth noting that your code will write "," at the end of each line. The library will not. Your code could be changed like this:
boolean more = keys.hasNext();
while (more) {
writer.append(keys.next().getCount());
more = keys.hasNext();
if(more) {
writer.append(',');
}
}
CSV seems simple, and usually is, until you start encountering more complex situations, such as quoted fields containing commas or escaped quotes:
A field,"another field","a field, containing a comma","A \"field\""
If your program encounters a situation like this, it will break, and you'd need to enhance your CSV algorithms to handle it. If you were using a library, you could have some reasonable expectation that it would handle quotes and quoted commas from the outset. It's up to you how likely you think that situation is.
Writing CSV code is usually simple, but there are pitfalls and it's always good to have less code to maintain.
Using a library has its own overheads -- managing the dependencies and so on.
You probably don't need a library for the simple stuff you're doing now. You might consider using one if your own code evolves to get more complicated, or if you start to need features like exporting beans to CSV, or handling CSV containing quoted commas.