0

I'm writing a program where I need to write data to csv. However the string what I'm writing to csv has some commas. So when I'm writing to it I'm getting values in different fields, which I don't want Eg: my string will come like : 165328,1234582,21346 I'm getting output as each value in one field, but I want the string as it is in one field

        while((s=lnr.readLine())!=null)
        {
                     str=s.split(" ");
                     br.write(str[7]);
        }

Please add the required thing to get the desired output. Eg : the string I'm writing will look like this 165328,123482,123414...

  • 3
    Duplicate. See http://stackoverflow.com/questions/12473480/how-should-i-escape-commas-and-speech-marks-in-csv-files-so-they-work-in-excel and http://stackoverflow.com/questions/4617935/is-there-a-way-to-include-commas-in-csv-columns-without-breaking-the-formatting and http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file – Ricardo Vila Sep 18 '15 at 12:56
  • for this one, I'm having diff string each time – Bharath Kumar Reddy Sep 18 '15 at 13:00

1 Answers1

4

Write the string which contains comma in double quotes and then write it into the csv file with File Writer or any writer you wish .

 value="\"" +value + "\"";
 fos.append(value);
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35