0

I want to read csv file with BufferedReader and split with StringTokenizer. However there is one line in file which contains this line :

ide,12,office,,3208.83,0.18,577.5,4876

Also here is my read method;

public void readFromCSV(){  
    try {
        File file = new File(myFile);
        br = new BufferedReader(new FileReader(file));
        String line = null;
        while((line = br.readLine()) != null) {
            st1 = new StringTokenizer(line,"\n");
            while(st1.hasMoreTokens()) {
                oneLineList = new ArrayList<>();
                st2 = new StringTokenizer(st1.nextToken(),",");
                for(int i = 0; i < 8; i++) {
                    oneLineList.add(st2.nextToken());
                }
                dataList.add(counter,oneLineList);
            }
            counter++;
        }
        br.close();
    }catch(IOException ex) {
        ex.printStackTrace();
    }
}

In for statement, there are 8 fields in each line, and dataList is a two-dimensional array list.

I cannot read whole data because of one line contain consecutive coma, how should I do for fix this?

MrBoolean
  • 29
  • 6
  • StringTokenizer skips "empty" tokens like in your example. If you are trying to get all the values between the commas, including empty string values, you can use String.split(","). This was answered in a previous question. http://stackoverflow.com/questions/20542992/how-to-read-two-consecutive-commas-from-csv-file-format-as-unique-value-in-java – dkoch74 Jan 25 '17 at 23:24
  • http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes – adhg Jan 25 '17 at 23:53

0 Answers0