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

gudnpqoy  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(354)

我需要读取java中的csv文件,并将信息添加到字符串数组列表中。问题是代码在读取csv文件时出错,但是如果我去掉名为“synopsis”的csv列,代码运行得很好。很明显,该专栏与代码失败有关,但我不知道是什么原因。有人有什么想法吗?
链接到google sheets版本的csvhttps://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)
yrdbyhpb

yrdbyhpb1#

使用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]);
    }
}
vngu2lb8

vngu2lb82#

你的代码很好-我相信-问题在于 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(制表符分隔)或其他分隔符,而不是逗号,可能类似于条形符号 | ?,因为您在列的内容中使用逗号。

相关问题