【发布时间】:2015-02-18 17:49:50
【问题描述】:
我有以下问题:我正在尝试在 java 中解析一个 .csv 文件,并将其中的 3 列专门存储在一个二维数组中。该方法的代码如下所示:
public static void parseFile(String filename) throws IOException{
FileReader readFile = new FileReader(filename);
BufferedReader buffer = new BufferedReader(readFile);
String line;
String[][] result = new String[10000][3];
String[] b = new String[6];
for(int i = 0; i<10000; i++){
while((line = buffer.readLine()) != null){
b = line.split(";",6);
System.out.println("ID: "+b[0]+" Title: "+b[3]+ "Description: "+b[4]); // Here is where the outofbounds exception occurs...
result[i][0] = b[0];
result[i][1] = b[3];
result[i][2] = b[4];
}
}
buffer.close();
}
我觉得我必须指定这一点:.csv 文件很大。它有 32 列,和(几乎)10.000 个条目(!)。 解析时,我不断收到以下信息:
XXXXX CHUNKS OF SUCCESFULLY EXTRACTED CODE
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:3
at ParseCSV.parseFile(ParseCSV.java:24)
at ParseCSV.main(ParseCSV.java:41)
但是,我意识到文件中的某些内容具有奇怪的格式,例如例如,其中的一些文本中有换行符,但没有以任何方式涉及换行符。但是,如果我手动删除这些空行,则生成的输出(在提示错误消息之前)会将这些内容添加到数组中,直到下一个空行... 有谁知道如何解决这个问题?任何帮助将不胜感激...
【问题讨论】:
标签: java csv indexoutofboundsexception