Configurable data exporter

445 Views Asked by At

I need to implement data exporter to CSV using java. It must meet next requirements:

1) it must be configurable (e.g. an order or number of columns must not be hard coded)

2) this data can be in different languages (also Chinese) and the resulting csv files must be utf-8 encoded.

I've never worked with such kind of tasks, so I just want to ask if somebody could provide me some useful advises: what are the best practices in configuring a data exporter and encoding data.

I've already found the following discussion about different csv exporter/importer CSV API for Java but if you have something to add, don't hesitate to do this ;)

And I've also heard something about drools, but have no clue yet could it be the solution in my case?

Thanks a lot for your replies!

1

There are 1 best solutions below

0
On

CSV is a really simple format. Here's basically the gist of encoding/decoding.

Encoding:

  • If the value contains no commas or newlines, just emit it as-is.
  • If the value contains a comma or newline, replace all / with // and all " with /", wrap it in quotes, and emit it.
  • Separate fields with commas and records with newlines.

Decoding:

  • If the value does not start with a quote, scan until you hit a comma.
  • If the value starts with a quote, scan through, replacing // with / and /" with " until you reach a quote that wasn't preceded by a single backslash.

You should not need to be worried about encoding; Java should handle that fine. But really, unless this is specifically for a school asignment, or something like that, you should be using a pre-existing CSV library.