Example usage for org.apache.commons.csv CSVParser CSVParser

List of usage examples for org.apache.commons.csv CSVParser CSVParser

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser CSVParser.

Prototype

public CSVParser(final Reader reader, final CSVFormat format) throws IOException 

Source Link

Document

Customized CSV parser using the given CSVFormat

If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .

Usage

From source file:com.fbartnitzek.tasteemall.data.csv.CsvFileReader.java

/**
 * reads CSV file in data and headers with same count, uses CSV_Format RFC4180 (, and "")
 * @param file file to read//from   w ww . j a  v  a2s  .  c om
 * @param headers expected columns
 * @return data
 */
public static List<List<String>> readCsvFileHeadingAndData(File file, List<String> headers) {

    List<List<String>> data = new ArrayList<>();

    CSVParser csvParser = null;
    Reader csvReader = null;
    try {
        csvReader = new FileReader(file);
        csvParser = new CSVParser(csvReader, CsvFileWriter.CSV_FORMAT_RFC4180.withHeader());
        Map<String, Integer> headerMap = csvParser.getHeaderMap();

        // print headerMap
        for (Map.Entry<String, Integer> entry : headerMap.entrySet()) {
            System.out.println(entry.getValue() + ": " + entry.getKey());
        }

        // should be same order!

        // 0 columns seems impossible, but valid

        // ordered columns instead unordered set (for insert)!
        headers.addAll(headerMap.keySet());
        for (CSVRecord record : csvParser) {
            List<String> dataEntry = new ArrayList<>();
            for (int i = 0; i < headers.size(); ++i) {
                if (i < record.size()) {
                    dataEntry.add(record.get(i));
                }
                //                    dataEntry.add(record.get(headers.get(i)));
            }
            data.add(dataEntry);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (csvReader != null) {
                csvReader.close();
            }
            if (csvParser != null) {
                csvParser.close();
            }
        } catch (IOException e) {
            //                System.out.println("Error while closing fileReader/csvFileParser !!!");
            e.printStackTrace();
        }
    }

    return data;
}

From source file:ca.liquidlabs.android.speedtestvisualizer.util.CsvDataParser.java

/**
 * Parses CSV data//from  w  w  w. jav a2  s . co m
 * 
 * @param csvHeader Header items for csv records
 * @param csvData
 * @return
 */
public static List<SpeedTestRecord> parseCsvData(String csvHeader, String csvData) {
    Reader in = new StringReader(getCsvData(csvHeader, csvData));
    try {
        CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT.toBuilder().withHeader().build());

        // get the parsed records
        List<CSVRecord> list = parser.getRecords();
        // create a list to convert data into SpeedTestRecord model
        List<SpeedTestRecord> speedTestRecords = new ArrayList<SpeedTestRecord>();
        for (CSVRecord csvRecord : list) {
            speedTestRecords.add(new SpeedTestRecord(csvRecord));
        }

        return speedTestRecords;
    } catch (IOException e) {
        Tracer.error(LOG_TAG, e);
    }
    // when no data, send empty list
    return Collections.emptyList();
}

From source file:main.StratioENEI.java

private static void readFromCSV(String filename, float[][] outCustos, List<String> outCidades)
        throws IOException {
    CSVParser c = new CSVParser(new FileReader(filename),
            CSVFormat.EXCEL.withDelimiter(';').withNullString(""));
    int lineNumber;
    for (CSVRecord record : c) {
        // System.out.println(record);

        if ((lineNumber = (int) record.getRecordNumber()) == 1) {
            continue;
        }// w ww.  j  av a 2 s. co  m
        outCidades.add(record.get(0));
        //     System.out.printf("\n%10s", record.get(0));
        for (int i = lineNumber - 1; i < outCustos.length + 1; i++) {
            //        System.out.printf("\t%-6s|", (record.get(i) == null) ? "null" : record.get(i));
            outCustos[lineNumber - 2][i - 1] = outCustos[i - 1][lineNumber - 2] = Float
                    .parseFloat((record.get(i) == null) ? "0.0" : record.get(i));
        }
    }
}

From source file:edu.caltech.ipac.firefly.server.util.DsvToDataGroup.java

public static DataGroup parse(File inf, CSVFormat format) throws IOException {

    BufferedReader reader = new BufferedReader(new FileReader(inf), IpacTableUtil.FILE_IO_BUFFER_SIZE);

    List<DataType> columns = new ArrayList<DataType>();
    CSVParser parser = new CSVParser(reader, format);
    List<CSVRecord> records = parser.getRecords();
    if (records != null && records.size() > 0) {

        // parse the column info
        CSVRecord cols = records.get(0);
        for (Iterator<String> itr = cols.iterator(); itr.hasNext();) {
            String s = itr.next();
            if (!StringUtils.isEmpty(s)) {
                columns.add(new DataType(s, null)); // unknown type
            }/*w  w w  . j a  v  a2 s.c  o  m*/
        }

        DataGroup dg = new DataGroup(null, columns);

        // parse the data
        for (int i = 1; i < records.size(); i++) {
            DataObject row = parseRow(dg, records.get(i));
            if (row != null) {
                dg.add(row);
            }
        }
        dg.shrinkToFitData();
        return dg;
    }
    return null;
}

From source file:cz.pichlik.goodsentiment.common.CSVReader.java

public CSVReader(InputStream input) {
    super();/*from  w ww  . j  a v  a  2  s . c o  m*/
    try {
        this.csvParser = new CSVParser(new InputStreamReader(input), CSVFormats.format());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.siemens.sw360.datahandler.common.ImportCSV.java

/**
 * reads a CSV file and returns its content as a list of CSVRecord
 *
 * @param in//from   ww  w  .  java2  s . co  m
 * @return list of records
 */
public static List<CSVRecord> readAsCSVRecords(InputStream in) {
    List<CSVRecord> records = null;

    try (Reader reader = new InputStreamReader(in)) {
        CSVParser parser = new CSVParser(reader, CommonUtils.sw360CsvFormat);
        records = parser.getRecords();
        records.remove(0); // Remove header
    } catch (IOException e) {
        log.error("Error parsing CSV File!", e);
    }

    // To avoid returning null above
    if (records == null)
        records = Collections.emptyList();

    return records;
}

From source file:algoritma.LoadData.java

public void read(String filePath, int confi) throws FileNotFoundException, IOException {
    FileReader fileReader = null;
    CSVParser csvFileParser = null;// ww w . ja  v  a 2 s.c  o m
    CSVFormat csvFileFormat = CSVFormat.EXCEL.withFirstRecordAsHeader();
    fileReader = new FileReader(filePath);
    csvFileParser = new CSVParser(fileReader, csvFileFormat);
    List csvRecords = csvFileParser.getRecords();

    for (int i = 0; i < csvRecords.size(); i++) {
        CSVRecord record = (CSVRecord) csvRecords.get(i);
        double confidence = Double.parseDouble(record.get("confidence"));
        if (confidence >= confi) {
            Point p = new Point();
            p.setBrightness(Double.parseDouble(record.get("brightness")));
            p.setBright_t31(Double.parseDouble(record.get("bright_t31")));
            p.setFrp(Double.parseDouble(record.get("frp")));
            p.add(p);
        }
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.CSVReader.java

/**
 * String tokeniser method will split up a CSV file by row and column 
 * returning a List<List<String>>
 * @param in/*w  ww .  j a  va2  s .  c  o  m*/
 * @param struct
 * @return
 */
public static ArrayList<ArrayList<String>> read(BufferedReader in, ArrayList<ColumnStructure> struct) {
    ArrayList<ArrayList<String>> dmap = new ArrayList<ArrayList<String>>();
    CSVParser parser = new CSVParser(in, CSVReader.strategy);

    //if header
    try {
        //[consume header]
        //String[] header = 
        parser.getLine();

        //and body
        String[] line = null;

        while ((line = parser.getLine()) != null) {
            ArrayList<String> list = new ArrayList<String>();

            for (int i = 0; i < line.length; i++) {
                ColumnStructure cs = struct.get(i);
                switch (cs) {
                case VC:
                    list.add(line[i]);
                    break;
                case TS:
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(DATA_DF.parse(line[i]));
                    list.add(ALUDBUtilities.ALUDB_DF.format(cal.getTime()));
                    break;
                case FL:
                    list.add(String.valueOf(validateFloat(Float.parseFloat(line[i]))));
                    break;
                case IT:
                    list.add(String.valueOf(validateInt(Integer.parseInt(line[i]))));
                    break;
                default:
                    list.add(line[i]);
                }
            }

            dmap.add(list);
        }
    } catch (IOException ioe) {
        // TODO Auto-generated catch block
        ioe.printStackTrace();
    } catch (ParseException pe) {
        // TODO Auto-generated catch block
        pe.printStackTrace();
    }

    return dmap;
}

From source file:net.decix.jatlasx.csv.CsvReader.java

public List<String> getColumn(String rowIdentifier) {

    CSVParser parser = null;//from ww w  .  j a  v a 2  s. c  o  m
    List<String> column = new ArrayList<String>();

    try {
        parser = new CSVParser(new FileReader(fileName), csvFileFormat);

        if (parser != null) {
            for (CSVRecord record : parser) {
                column.add(record.get(rowIdentifier).toString());
            }
        }

        parser.close();
    } catch (IOException e) {
        String errorMsg = "Could not read File:" + fileName;
        System.err.println(e.getClass().getName() + ":" + errorMsg + " (" + this.getClass().getName() + ")");
    }
    return column;
}

From source file:com.mycompany.couchdb.CsvManage.java

public boolean hello() throws FileNotFoundException, IOException {
    int count = 0;
    String filename = "src/main/resources/GeoLiteCity-Location.csv";
    City city;/*from   w ww  .ja v a  2s .  c om*/
    DataDealer cityDao;

    cityDao = new DataDealer();

    Reader in = new FileReader(filename); //read file
    CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader()); //create parser
    city = new City();
    for (CSVRecord record : parser) {
        //parse file and create objects

        //city.setLocId(Integer.parseInt(record.get("locId")));
        //city.setCountry(record.get("country"));
        city.setCity(record.get("city"));
        //city.setRegion(record.get("region"));
        city.setLatitude(Float.parseFloat(record.get("latitude")));
        city.setLongitude(Float.parseFloat(record.get("longitude")));

        //

        try {
            cityDao.save(city);

        } finally {
            cityDao.finalize();
        }

    }
    return true;
}