Link to home
Start Free TrialLog in
Avatar of royalcyber
royalcyber

asked on

How to read data from a .csv file

I have an application where I used Jakarta POI-HSSF API to read data from an excel .xls file. I pass the fileInputStream to the method and it gives me an arraylist of rocords. One record contains an arraylist of cells.

Now instead of reading data from a .xls file, I need t read data from a .csv file, but I am not sure how to read data from a .csv file. I would greatly appreciate if somebody can recommend an API or have code for this.

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mukundha_expert
mukundha_expert

This Code will Get a ArrayList of Records from the CSV file and each record inturn will contain a arraylist of cells. This is working fine,


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.StringTokenizer;

public class CSV {

      public static void main(String[] args)
                  throws IOException{
            
            FileReader fileR = new FileReader("test.csv");
            BufferedReader bCsv = new BufferedReader(fileR);
            String out="";
            ArrayList record = new ArrayList();
            //Adding Recors to the ArrayList Record
            while(out!=null){
                  out = bCsv.readLine();
                  
                  if(out==null){
                        break;
                  }
                  //Adding cells to the ArrayList cells
                  ArrayList cells = new ArrayList();
                  StringTokenizer tokens = new StringTokenizer(out,",");
                  while(tokens.hasMoreElements()){
                        cells.add(tokens.nextToken());
                  }
                  record.add(cells);
            }
            
            //Iterating thro the Records
            ListIterator recordIterator = record.listIterator();
            
            while(recordIterator.hasNext())      {
                  ArrayList cell = (ArrayList)recordIterator.next();
                  
                  ListIterator cellIterator = cell.listIterator();
                  while(cellIterator.hasNext()){
                        System.out.print((String)cellIterator.next()+" ");
                  }
                  System.out.println("");
            }
      }
}


Cheers,
Mukundh      
notice that using StringTokenizer is not the proper way of reading csv files because of its weakness in both handling empty strings and also tokenizing quoted strings that have commas inside them
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of royalcyber

ASKER

Thank you very much guys for all your replies

Mukhand I tried your code but somehow it gives me the following error

String index out of range: -1399

would you know why I am getting this error

Thanks
I tried in my system. its not showing any errors. I created the .csv file using Excel. If you could send ur .csv file to my mail. i can check it and tell you. <mail removed by Venabili>

cheers,
Mukundh
No e-mail IDs on question-pages please.