-1

I am writing a CSV parts.Its easy to write it using java

String s = null
while( (s= bufferedReadr.readLine() !=null){
  String parts[] = s.split(',');
  //process parts
}

The problem is data is in format
"a", "b","c","d","e","f".

But some of the data is
"a" ,",b" ,"c",",a","e","f" .

EDIT

The thing is i get one line of CSV input from some other code which i cant change.

SO i get each line which is in CSV format. And i want to split that.I cant use csvlibs as they parse whole file and requie a handle to file.

some quote are present withing data parts which is causing problem.

How do i resolve this?Any open src libs i can use?

user93796
  • 18,749
  • 31
  • 94
  • 150

2 Answers2

2

Do not split data yourself. Use one of available libraries that do this job well. For example take a look on opencsv.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • :Can you comment on my latets edit – user93796 Jan 23 '14 at 14:51
  • @user93796, no problem. Check the API. It should allow passing stream or reader. You can always wrap string using `StringReader` and use CSV parser with single line. If you however want to work hard and pare it yourself you can play with positive/nagative lookahead `(?!X)`, but it seems to be complicated. I personally do not know how to write such regex. Alternatively you can split string yourself by iterating on characters and calling `substring()`. This will work. – AlexR Jan 23 '14 at 15:07
  • I used apache commons CSVReader finally. – user93796 Jan 24 '14 at 10:41
  • @user93796, CSVReader is a good choice. Good luck. – AlexR Jan 26 '14 at 07:27
1

As the other comments say, use OpenCSV. There is the CSVReadr class with this contructor

public CSVReader(Reader reader, char separator, char quotechar)
And methods like readAll(), readNext(), .... About the Reader, there are many different options:.., InputStreamReader, StringReader
XaviMuste
  • 194
  • 4