10

Possible Duplicate:
Get and Parse CSV file in android

I would like to store a csv file in android app itself & it should be called for read & later print the values according to the requirements.I definitely want the csv file to be called inside the app not outside the app[SD Card].Any examples on this will be really helpful.Thanks

Community
  • 1
  • 1
Karthik
  • 4,943
  • 19
  • 53
  • 86
  • Drop the CSV files into your Assets folder and use the OpenCSV library http://opencsv.sourceforge.net/ to parse them. – LuxuryMode Dec 14 '11 at 04:09
  • I use Java CSV for my project. It is fast and light weight Link: http://sourceforge.net/projects/javacsv/ – anirus Aug 13 '12 at 23:05

1 Answers1

10

I have something like this;

public final List<String[]> readCsv(Context context) {
  List<String[]> questionList = new ArrayList<String[]>();
  AssetManager assetManager = context.getAssets();

  try {
    InputStream csvStream = assetManager.open(CSV_PATH);
    InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
    CSVReader csvReader = new CSVReader(csvStreamReader);
    String[] line;

    // throw away the header
    csvReader.readNext();

    while ((line = csvReader.readNext()) != null) {
      questionList.add(line);
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
  return questionList;
}
Matthew Rudy
  • 16,724
  • 3
  • 46
  • 44
  • Is the CSVReader an external jar...if so do you have an authenticated link for it..thanks for your reply.. – Karthik Dec 14 '11 at 05:10
  • 1
    The same as LuxuryMode suggested. http://opencsv.sourceforge.net/ – Matthew Rudy Dec 14 '11 at 06:02
  • Thanks for the reply but my csv is of 1.5 mb size.When I give it I am not getting anything,but I am getting the output when on lower sizes.Do there is any constraint for csv size. – Karthik Dec 14 '11 at 06:22
  • If you're getting nothing, I guess you're hitting the `catch` and doing the `e.printStackTrace()`. I suggest you look through your log to find this stack trace. Maybe you overflow some buffer, or maybe there's a bad character in there somewhere. – Matthew Rudy Dec 14 '11 at 06:51
  • Yes I am getting this in the logcat Data exceeds UNCOMPRESS_DATA_MAX (1665199 vs 1048576),why cant I use any file larger than 1 mb. – Karthik Dec 14 '11 at 06:55
  • I suggest look at the documentation on sourceforge. The limit will be there for a reason (especially in android, system memory is precious). There may be a better way to stream it in chunks. Or you may just be able to override the limit. – Matthew Rudy Dec 14 '11 at 07:00
  • Is it possible to override the limit,if so please give me snippet on it.If I increase it what will be a permissible amount on it. – Karthik Dec 14 '11 at 07:08