Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing comma-separated values enclosed with quotes

Tags:

java

csv

I'm trying to parse comma separated values that are enclosed in quotes using only standard Java libraries (I know this must be possible)

As an example file.txt contains a new line for each row of

"Foo","Bar","04042013","04102013","Stuff"
"Foo2","Bar2","04042013","04102013","Stuff2"

However when I parse the file with the code I've written so far:

import java.io.*;
import java.util.Arrays;
 public class ReadCSV{

    public static void main(String[] arg) throws Exception {

        BufferedReader myFile = new BufferedReader(new FileReader("file.txt"));

        String myRow = myFile.readLine(); 
        while (myRow != null){
            //split by comma separated quote enclosed values
            //BUG - first and last values get an extra quote
            String[] myArray = myRow.split("\",\""); //the problem

            for (String item:myArray) { System.out.print(item + "\t"); }
            System.out.println();
            myRow = myFile.readLine();
        }
        myFile.close();
    }
}

However the output is

"Foo    Bar     04042013        04102013        Stuff"

"Foo2   Bar2    04042013        04102013        Stuff2"

Instead of

Foo    Bar     04042013        04102013        Stuff

Foo2   Bar2    04042013        04102013        Stuff2

I know I went wrong on the Split but I'm not sure how to fix it.

like image 697
sputn1ck Avatar asked Dec 03 '25 16:12

sputn1ck


1 Answers

Before doing split, just remove first double quote and last double quote in myRow variable using below line.

myRow = myRow.substring(1, myRow.length() - 1);

(UPDATE) Also check if myRow is not empty. Otherwise above code will cause exception. For example below code checks if myRow is not empty and then only removes double quotes from the string.

if (!myRow.isEmpty()) {
    myRow = myRow.substring(1, myRow.length() - 1);
}
like image 147
Niraj Nawanit Avatar answered Dec 06 '25 06:12

Niraj Nawanit