Java CSV File Load importCsv(File file)

Here you can find the source of importCsv(File file)

Description

Convert the csv into an array of String[].

License

Open Source License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static String[][] importCsv(File file) throws Exception 

Method Source Code

//package com.java2s;
/*/*  w  w w. j ava  2 s. c o m*/
 * Spirit, a study/biosample management tool for research.
 * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91,
 * CH-4123 Allschwil, Switzerland.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * @author Joel Freyss
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.LineNumberReader;
import java.io.Reader;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    /**
     * Convert the csv into an array of String[]. The dimension of each row can vary depending of the size of the splits
     * Example:
     * <pre>
     * File example
     *
     * H1, H2, H3
     * D1, "D1,Dd", D3
     * <pre>
     * will return:
     * <pre>
     * [[File Example], [H1, H2, H3], [D1, 'D1,dd', D3]]
     * </pre>
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static String[][] importCsv(File file) throws Exception {
        if (!file.exists())
            throw new FileNotFoundException(file.getAbsolutePath());
        return importCsv(new FileReader(file));
    }

    public static String[][] importCsv(Reader r) throws Exception {
        List<String[]> lines = new ArrayList<String[]>();
        try (LineNumberReader reader = new LineNumberReader(r)) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty()) {
                    continue;
                }
                String[] split = split(line, ",;\t");
                lines.add(split);
            }
        }
        return lines.toArray(new String[lines.size()][]);
    }

    public static String[] split(String s, String separators) {
        if (s == null)
            return new String[0];
        StringTokenizer st = new StringTokenizer(s, "\"" + separators, true);
        List<String> res = new ArrayList<String>();
        boolean inQuote = false;
        StringBuilder sb = new StringBuilder();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (token.equals("\"")) {
                if (inQuote) {
                    //               if(sb.toString().trim().length()>0) res.add(sb.toString().trim());
                    //               sb.setLength(0);
                    inQuote = false;
                } else {
                    inQuote = true;
                }
            } else if (!inQuote && separators.indexOf(token) >= 0) {
                res.add(sb.toString().trim());
                sb.setLength(0);
            } else {
                sb.append(token);
            }
        }
        res.add(sb.toString().trim());

        return res.toArray(new String[res.size()]);
    }
}

Related

  1. importCsv(File file)
  2. importCsv(File file)