使用OpenCsv时csvReader行出错

时间:2018-04-16 18:09:36

标签: java csv

我的代码出错,我不明白为什么。我正在使用OpenCsv库并尝试将我的CSV保存到数组中。 CSV的列类似于:日期,已销售,代码,然后是其下的信息,如:1123,may4,0021; 3323;

我的代码是:

 private static final String SAMPLE_CSV ="C:\\Users\\Documents\\o.csv\\";

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


     { 

        try (
            Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV));
            CSVReader csvReader = new CSVReader(reader);
        ) {
            // Reading Records One by One in a String array
            String[] nextRecord;
            while ((nextRecord = csvReader.readNext()) != null) {
                System.out.println("Order num : " + nextRecord[0]);
                System.out.println("SoldToAct : " + nextRecord[1]);
                System.out.println("RequestedDelivery : " + nextRecord[2]);
                System.out.println("BaseCode : " + nextRecord[3]);
                 System.out.println("Option Code : " + nextRecord[4]);
                System.out.println("==========================");
            }
        }

错误发生在CSVReader中csvReader = new CSVReader(reader);线。

1 个答案:

答案 0 :(得分:0)

尝试删除SAMPLE_CSV

末尾的双反斜杠

更改自:

private static final String SAMPLE_CSV ="C:\\Users\\Documents\\o.csv\\";

对此:

private static final String SAMPLE_CSV ="C:\\Users\\Documents\\o.csv";