Java CSV File Parse parseCSV(String csvString)

Here you can find the source of parseCSV(String csvString)

Description

Parses a given multi-line CSV-formated string to a String matrix.

License

Open Source License

Parameter

Parameter Description
csvString CSV string.

Return

Matrix.

Declaration

public static String[][] parseCSV(String csvString) 

Method Source Code

//package com.java2s;
/* Util.java/* ww  w. j  a v a2s. c om*/
 * 
 * Networking ME
 * Copyright (c) 2013 eMob Tech (http://www.emobtech.com/)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.io.UnsupportedEncodingException;

import java.util.Vector;

public class Main {
    /**
     * <p>
     * UTF-8 encoding.
     * </p>
     */
    public static final String UTF8 = "UTF-8";

    /**
     * <p>
     * Parses a given multi-line CSV-formated string to a String matrix.
     * </p>
     * @param csvString CSV string.
     * @return Matrix.
     */
    public static String[][] parseCSV(String csvString) {
        if (isEmptyString(csvString)) {
            return new String[0][];
        }
        //
        char c = '0';
        boolean valueStarted = false;
        boolean stringValueStarted = false;
        char[] csvArray = csvString.toCharArray();
        //
        Vector lines = new Vector();
        Vector values = new Vector();
        StringBuffer value = new StringBuffer();
        //
        lines.addElement(values);
        //
        for (int i = 0; i < csvArray.length; i++) {
            c = csvArray[i];
            //
            if (valueStarted) {
                if (c == '\"') {
                    if (isPairOfDoubleQuote(csvArray, i)) {
                        value.append('\"');
                        //
                        i++;
                    } else {
                        stringValueStarted = false;
                    }
                } else {
                    if (stringValueStarted || (c != ',' && c != '\n')) {
                        if (c != '\r') {
                            value.append(c);
                        }
                    } else {
                        values.addElement(value.toString());
                        //
                        valueStarted = false;
                        value.setLength(0);
                        //
                        if (c == '\n') {
                            values = new Vector();
                            lines.addElement(values);
                        }
                    }
                }
            } else {
                if (c == ',') {
                    values.addElement("");
                } else if (c == '\n') {
                    values = new Vector();
                    lines.addElement(values);
                } else if (!isWhiteSpaceChar(c)) {
                    valueStarted = true;
                    //
                    if (c == '\"') {
                        if (isPairOfDoubleQuote(csvArray, i)) {
                            value.append('\"');
                            //
                            i++;
                        } else {
                            stringValueStarted = true;
                        }
                    } else {
                        if (c != '\r') {
                            value.append(c);
                        }
                    }
                }
            }
        }
        //
        if (value.length() > 0) {
            values.addElement(value.toString());
        } else if (c == ',' || isWhiteSpaceChar(c)) {
            values.addElement("");
        }
        //
        if (values.size() == 0) {
            lines.removeElementAt(lines.size() - 1);
        }
        //
        String[] line;
        String[][] csv = new String[lines.size()][];
        //
        for (int i = 0; i < csv.length; i++) {
            values = (Vector) lines.elementAt(i);
            //
            line = new String[values.size()];
            values.copyInto(line);
            //
            csv[i] = line;
        }
        //
        return csv;
    }

    /**
     * <p>
     * Checks whether the given string is null or empty.
     * </p>
     * @param str The string.
     * @return true null/empty.
     */
    public static boolean isEmptyString(String str) {
        return str == null || str.trim().length() == 0;
    }

    /**
     * Returns if a given index represents a pair of double-quotes.
     * @param chars Chars.
     * @param index Index.
     * @return Pair (true).
     */
    private static boolean isPairOfDoubleQuote(char[] chars, int index) {
        return chars[index] == '\"' && index + 1 < chars.length && chars[index + 1] == '\"';
    }

    /**
     * <p>
     * Returns the string of a given bytes.
     * </p>
     * @param bytes Bytes.
     * @return String.
     */
    public static String toString(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        }
        //
        try {
            return new String(bytes, UTF8);
        } catch (UnsupportedEncodingException e) {
            return new String(bytes);
        }
    }

    /**
     * <p>
     * Returns if a given char is a space or tab.
     * </p> 
     * @param c Char.
     * @return is space char or not.
     */
    public static final boolean isWhiteSpaceChar(char c) {
        return c == ' ' || c == '\t';
    }
}

Related

  1. parse(final String csvString)
  2. parseCsv(InputStream csvInput)
  3. parseCsv(InputStream fileStream)
  4. parseCSV(String csv)
  5. parseCSV(String csv)
  6. parseCSV(String data, char... ch)
  7. parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)
  8. parseCSVIntegers(String csv)
  9. parseCsvLine(final String line)