5

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.

2
  • What happens when you read a line from CSV and split it using "\t\t" ? Commented Apr 14, 2014 at 10:43
  • That's what I did finally, I parsed the file manually line by line and split by double tab string Commented Apr 16, 2014 at 8:49

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i agree making a custom JAVA CSV reader is preferable than using APIs.
I don't agree that reading/parsing csv is such a simple thing. It get's quite fast nasty (eg. quoted strings containing the field separator). But he could use openCSV and simply subclass CSVParser.parseLine().
Suggest it as an answer @DirkLachowski
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) {}

Comments

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] = "";
...

1 Comment

I don't think this will help, because with Jsefa I'm mapping the extracted line to the corresponding object
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!

Comments

0

The Apache commons-csv module added support for String delimiters in v1.9.0 a couple of years ago.

https://issues.apache.org/jira/browse/CSV-206

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.