除非删除特定列,否则无法从 CSV 文件读取文本

时间:2021-07-04 10:22:22

标签: java csv

我需要从 Java 中读取 CSV 文件,并将信息添加到字符串数组列表中。问题是代码在读取 CSV 文件时出错,但如果我取出名为“概要”的 CSV 列,代码运行完美。很明显该列与代码失败有关,但我无法弄清楚是什么。有人有什么想法吗?

链接到 CSV 的谷歌表格版本 https://docs.google.com/spreadsheets/d/1EO243KiaZ_uxEKF1mvozONHvTBm5HfFsUvrR5SSB238/edit?usp=sharing

代码在这里-->

package testerproject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class testerpage {
    private List<String[]> list = new ArrayList();
    testerpage(){
        try {
            animeList();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    private void  animeList() throws IOException {
        File file = new File("resources/CSVFiles/animeProjectData.csv");
        int count = 0; //keep track of where in the list new line is added
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = "";
            //add line from CSV file to specified list index
            while ((line = br.readLine()) != null) {
                list.add(count,line.split(",")); 
                count++;
                }
            } catch (FileNotFoundException e) {
        }           
        System.out.println(list.get(2)[3]);
    }
}

给出的错误 ->

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 1
    at testerproject.testerpage.animeList(testerpage.java:37)
    at testerproject.testerpage.<init>(testerpage.java:17)
    at testerproject.tester.main(tester.java:6)

2 个答案:

答案 0 :(得分:1)

使用 opencsv 读取 csv 文件,我尝试了您共享的 csv 文件,并且可以使用 opencsv 读取它而没有任何错误,更多参考https://www.baeldung.com/opencsv

import com.opencsv.CSVReader;

public class testerpage {
    private List<String[]> list = new ArrayList();
    testerpage(){
        try {
            animeList();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    private void  animeList() throws IOException {
        File file = new File("resources/CSVFiles/animeProjectData.csv");
        int count = 0; //keep track of where in the list new line is added
        try{
            Reader reader = Files.newBufferedReader(file.toPath());
            CSVReader csvReader = new CSVReader(reader);
            String[] line;
            while ((line = csvReader.readNext()) != null) {
                list.add(line);
                count++;
            }
            reader.close();
            csvReader.close();
            } catch (FileNotFoundException e) {
        }           
        System.out.println(list.get(2)[3]);
    }
}

答案 1 :(得分:0)

您的代码很好,我相信问题在于您在此处共享的电子表格中 C:3 [2][3] 处的逗号

.
.
.
when he meets a beautiful violinist, Kaori Miyazono, who stirs up his 
           
world and sets him on a journey to face music again. 
 
Based on the manga series of the same name,  Shigatsu wa Kimi no 
                                                              
Uso  approaches the story of Kousei's recovery as he discovers that 
.
.
.

此文件不能用逗号分隔 我建议将工作表转换为 TSV(制表符分隔)或其他分隔符而不是逗号,可能是条形符号 | 之类的东西?,因为您在其中使用了逗号列的内容。