Search code examples
javafilecsvioopencsv

Java: parsing CSV file with OpenCSV


I am trying to parse CSV file with OpenCSV. I have an error while creating new CSVReader "CSVReader() in CSVReader cannot be applied to (Java.io.FileReader, char)". I copied this example from the official page. Why i cannot use a Reader? I have to access a csv file.

package csv;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {
    public static void main(String[] args) throws IOException {

        String fileName = "storici/eurusd.csv";

        CSVReader reader = new CSVReader(new FileReader(fileName), ',');

        List<CSVData> records = new ArrayList<CSVData>();

        // read line by line
        String[] record = null;

        while ((record = reader.readNext()) != null) {
            CSVData data = new CSVData();
            data.setTime(record[0]);
            data.setOpen(record[1]);
            data.setHigh(record[2]);
            data.setLow(record[3]);
            data.setClose(record[4]);
            data.setVolume(record[5]);
            records.add(data);
         }

        System.out.println(records);

        reader.close();
    }
}

Solution

  • The issue here is that you have called your class CSVReader which clashes with the OpenCSV class. I would suggest renaming your class then importing the correct one.