parse CSV Line - Java File Path IO

Java examples for File Path IO:CSV File

Description

parse CSV Line

Demo Code


//package com.java2s;

public class Main {
    public static String[] parseCSVLine(String line) {
        String[] field = new String[12];
        String temp = "";
        int index = 0;
        boolean hasQuotation = false;
        for (int i = 0; i < line.length(); i++) {
            Character currentChar = line.charAt(i);
            if (currentChar.equals('\"')) {
                hasQuotation = !hasQuotation;
            }//from  w ww . ja v  a  2  s .  c  o  m
            if (currentChar.equals(',')) {
                if (!hasQuotation) {
                    field[index] = temp;
                    temp = "";
                    index = index + 1;
                } else {
                    temp = temp + line.charAt(i);
                }
            } else {
                temp = temp + line.charAt(i);
            }
            if (i == line.length() - 1) {
                field[index] = temp;
                temp = "";
                index = 0;
            }
        }
        return field;
    }
}

Related Tutorials