3

I tried to parse a CSV file with two APIs - JSefa and OpenCSV - but the problem is that the separator in the CSV file is a double tab "\t\t" and the APIs only accept a character, not a string, for the separator.

Is there any other API that could solve the problem, or there s a way to determine a new String separator in Jsefa or OpenCSV?

As a last resort I could, before parsing, try to replace the double tab by a semicolon but I was hoping to do it a cleaner way.

Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
Jad B.
  • 1,403
  • 15
  • 14

4 Answers4

2

There's 101 examples online. I Googled "java parse csv" and this was the #1 result:

http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/

That uses a String as a separator, however this is code rather than an API.

Given that parsing a CSV file is a pretty simple process, I don't think it's a good example of a situation requiring a library and - especially with a slightly unique requirement like you have with the strange \t\t separator - it is probably better to just code it anyway.

Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
0

try this:

ArrayList<String> itemsOne=new ArrayList<String>();
ArrayList<String> itemsTwo=new ArrayList<String>();

InputStream is = getResources().openRawResource(R.raw.csvfile);
try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    String line;

    while ((line = reader.readLine()) != null ) {
        String [] rowData = line.split("\t\t");
        itemsOne.add(rowData[0]);
        itemsTwo.add(rowData[1]);
    }
} catch (Exception e) {}
The Badak
  • 2,010
  • 2
  • 16
  • 28
0

yes its correct that opencsv does not support String as deleimiter.

one simple solution is to use '\t' as delimiter and ignore empty fields.

for example :

String s = "1\t\t2\t\t3\t\t4";

to

String[7] fields;
fields[0] = "1";
fields[1] = "";
fields[2] = "2";
fields[3] = "";
...
Farvardin
  • 5,336
  • 5
  • 33
  • 54
  • I don't think this will help, because with Jsefa I'm mapping the extracted line to the corresponding object – Jad B. Apr 14 '14 at 12:32
0

The solution you are suggesting (replacing the separator string by a character seems to be the best one, as is easy and solves your problem. CSV is a very simple format, if you modify it adding complex separators then it is not a "Comma Separated Values" format anymore. Keep it simple!

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59