Java CSV File Parse parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)

Here you can find the source of parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)

Description

parse Csv File

License

Open Source License

Declaration

public static ArrayList<Float> parseCsvFile(String filename, String csvSplitBy, boolean skipHeader) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * QBiC Offer Generator provides an infrastructure for creating offers using QBiC portal and
 * infrastructure. Copyright (C) 2018 Benjamin Sailer
 *
 * 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/.
 *******************************************************************************/

import java.io.*;
import java.util.ArrayList;

public class Main {
    public static ArrayList<Float> parseCsvFile(String filename, String csvSplitBy, boolean skipHeader) {

        ArrayList<Float> parsedCsvFile = new ArrayList<>();
        // add a placeholder for the discount to skip the first value, so we can easily access the discount via
        // arrayList[5] -> discount for 5 samples
        parsedCsvFile.add(-1.0f);/*from  w  ww  . j  a  va2  s. c o  m*/

        try {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            try {
                if (skipHeader)
                    br.readLine();
                String line;
                while ((line = br.readLine()) != null) {
                    String[] lineSplitted = line.split(csvSplitBy);
                    parsedCsvFile.add(Float.parseFloat(lineSplitted[1])); //
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return parsedCsvFile;
    }
}

Related

  1. parseCsv(InputStream fileStream)
  2. parseCSV(String csv)
  3. parseCSV(String csv)
  4. parseCSV(String csvString)
  5. parseCSV(String data, char... ch)
  6. parseCSVIntegers(String csv)
  7. parseCsvLine(final String line)
  8. parseCSVLine(String CSVLine, char delimChar, char quotChar)
  9. parseCsvRecord(String record, char csvSeparator)