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

dev.spiti.utility.datareader.readers.CSV Maven / Gradle / Ivy

The newest version!
package dev.spiti.utility.datareader.readers;

import com.opencsv.CSVReader;

import java.io.FileReader;
import java.util.*;
import java.util.stream.Collectors;

public class CSV extends BaseReader {

    private static CSVReader READER;
    private static final String EXTENSION = ".csv";


    public CSV() {

    }

    public CSV(String file) {
        this.READER = readFile(file);
    }

    public CSVReader readFile(String file)  {
        CSVReader reader = null;
        try{
            reader =  new CSVReader(new FileReader(getPath() + file + EXTENSION));
        } catch (Exception e) {
            LOGGER.error("Error is reading CSV file " + e.getMessage());
            e.printStackTrace();
        }
        return reader;
    }

    private List> getDataAsList() {
        List> dataAsList = new ArrayList<>();
        String[] line;
        try{
            while((line = READER.readNext()) != null) {
                dataAsList.add(Arrays.asList(line));
            }
        } catch (Exception e) {
            LOGGER.error("Error is reading CSV data row " + e.getMessage());
            e.printStackTrace();
        }
        List header = dataAsList.get(0);
        for(int i=1; i rowData = dataAsList.get(i);
            if (header.size() == rowData.size()) {
                Map map = new HashMap<>();
                for(int j=0; j




© 2015 - 2024 Weber Informatics LLC | Privacy Policy