0

i am working with android app to import the csv file to populate my DB.

I am using OpenCSV library to do it. I wish to use CSV reader to read from the inputstream.

I am using import feature from the gmail, so i use android:scheme=content it makes csv file come as InputStream.

Inside I have 3 columns. Some rows have , character.

So when I try to separate columns using , I get error.

I wish help to know how to overcome this issue of comma inside the columns.

radoh
  • 4,554
  • 5
  • 30
  • 45
Sunil
  • 521
  • 1
  • 7
  • 24

2 Answers2

1

You could get your csv with another separator character? if yes, the you can call yor csv reader with this separator, from de docs...

    CSVReader(Reader reader, char separator)
       Constructs CSVReader with supplied separator.
jonhid
  • 2,075
  • 1
  • 11
  • 18
0

If there is comma in columns, it gets difficult to read a csv file properly, I will show you the code that doesn't requires any library:

   File f=new File("C:/Users/Public/"+filename+".csv");             

            Scanner sc=new Scanner(fc);
            sc.useDelimiter("\r");
            char cd='"';

            //Create scanner string seperator ","

            String g=String.valueOf(cd)+","+String.valueOf(cd);

            //JOptionPane.showMessageDialog(null, "G: "+g);

            String[] data=null;

            while(sc.hasNextLine()){

                data=null;

                csvtemp=sc.nextLine().toString();
            //remove first character ", string from second character                    
                csvtemp=csvtemp.subString(1);
                //remove last character ", string from first character to secondlast
                csvtemp=csvtemp.subString(0,csvtemp.length()-1);
                //Split record by seperator ","  and copy to array
                data=csvtemp.split(g);
            }   


            sc.close();