【发布时间】:2020-01-09 06:54:37
【问题描述】:
我在尝试读取 csv 文件时遇到问题,错误显示如下:java.lang.ArrayIndexOutOfBoundsException: length=1; index=1。我试过在 StackOverflow 中搜索,错误和上面一样。
代码:
private void readCSVFile(String csvNameFile) {
try {
File readFolderCSV = new File(Environment.getExternalStorageDirectory() + "/Download/" + csvNameFile);
CSVReader csvReader = new CSVReader(new FileReader(readFolderCSV.getAbsoluteFile()));
String[] nextLine;
while((nextLine = csvReader.readNext()) != null) {
Log.e("TAG", nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3] + nextLine[4]);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MasterUoM.this, "The specified file was not found", Toast.LENGTH_SHORT).show();
}
}
我的CSV文件:
【问题讨论】:
-
这意味着您在该数组中只有一项(在索引 0 处),但您正在尝试访问第二项(索引 1)。因此,
nextLine[1]很可能会导致Exception,因为其中只有一个String(可能是整行?)。 -
根据您的屏幕截图,您的 nextLine[] 数组中似乎只有 2 个对象。所以 nextLine[2] 或以上应该按预期给出一个 ArrayIndexOutOfBoundException 。
-
您的
csv每行仅包含 2 列,但您尝试获取 3、4、5 导致异常 -
@Md.Asaduzzaman MyCSVFile 只是一个例子。当我在类别名称列中有很多类别名称数据时怎么样?