All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bonitasoft.engine.tracking.csv.CSVUtil Maven / Gradle / Ivy

There is a newer version: 10.2.0
Show newest version
/**
 * Copyright (C) 2019 Bonitasoft S.A.
 * Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library 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 Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 **/
package org.bonitasoft.engine.tracking.csv;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class CSVUtil {

    public static List> readCSV(final boolean excludeHeader, final File csvFile, final String csvSeparator)
            throws FileNotFoundException {
        final List> array = new ArrayList>();
        final InputStream inputStream = new FileInputStream(csvFile);
        final Scanner scanner = new Scanner(inputStream);
        try {
            while (scanner.hasNextLine()) {
                final String line = scanner.nextLine();
                final List lineElements = new ArrayList();
                lineElements.addAll(Arrays.asList(line.split(csvSeparator)));
                array.add(lineElements);
            }
        } finally {
            scanner.close();
            try {
                inputStream.close();
            } catch (final IOException e) {
                throw new RuntimeException(e);
            }
        }

        if (excludeHeader) {
            array.remove(0);
        }
        return array;
    }

    public static void writeCSVRow(final FileWriter writer, final List row, final String csvSeparator)
            throws IOException {
        boolean first = true;
        for (final String value : row) {
            if (first) {
                writer.append(value);
                first = false;
            } else {
                writer.append(csvSeparator);
                writer.append(value);
            }
        }
        writer.append("\n");
        writer.flush();
    }

    public static void writeCSVRow(final File file, final List row, final String csvSeparator)
            throws IOException {
        final FileWriter writer = new FileWriter(file, true);
        writeCSVRow(writer, row, csvSeparator);
        writer.close();
    }

    public static void writeCSVRows(final File file, final List> array, final String csvSeparator)
            throws IOException {
        final FileWriter writer = new FileWriter(file, true);
        for (final List row : array) {
            writeCSVRow(writer, row, csvSeparator);
        }
        writer.close();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy