Java:读取.csv文件并保存到数组中

Dim*_*cha 3 java csv arrays

当我尝试读取 .csv 文件并将每列保存到数组中时,我遇到了异常问题。\n虽然程序看起来很长,但事实并非如此。我只有 15 个不同的数组。

\n\n

这是行中的异常“Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2”

\n\n
\n

部门[i] = dataArray[2];

\n
\n\n

有什么我可以做的吗?

\n\n
      BufferedReader CSVFile = \n            new BufferedReader(new FileReader("Sub-Companies.csv"));\n\n      String dataRow = CSVFile.readLine();\n      // Read the number of the lines in .csv file \n      // i = row of the .csv file\n      int i = 0; \n      while (dataRow != null){\n          i++;\n          dataRow = CSVFile.readLine();\n\n        }\n      System.out.println(i);\n      // Close the file once all data has been read.\n      CSVFile.close();\n\n      // End the printout with a blank line.\n      System.out.println();\n\n      // Save into arrays\n      customer_id = new String[i];\n      company_name = new String[i];\n      department = new String[i];\n      employer = new String[i];\n      country = new String[i];\n      zipcode = new String[i];\n      address = new String[i];\n      city = new String[i];\n      smth1 = new String[i];\n      smth2 = new String[i];\n      phone_no1 = new String[i];\n      phone_no2 = new String[i];\n      email = new String[i];\n      website = new String[i];\n      customer_no = new String[i];\n\n      // Read first line.\n      // The while checks to see if the data is null. If \n      // it is, we\'ve hit the end of the file. If not, \n      // process the data.\n      int j;\n      int counter;\n      i = 0;\n\n      // Read the file again to save the data into arrays\n      BufferedReader CSV = \n            new BufferedReader(new FileReader("Sub-Companies.csv"));\n\n      String data = CSV.readLine();\n\n      while (data != null){\n          String[] dataArray = data.split(";");\n          for (String item:dataArray) {\n            customer_id[i] = dataArray[0];\n            company_name[i] = dataArray[1];\n            department[i] = dataArray[2];\n            employer[i] = dataArray[3];\n            country[i] = dataArray[4];\n            zipcode[i] = dataArray[5];\n            address[i] = dataArray[6];\n            city[i] = dataArray[7];\n            smth1[i] = dataArray[8];\n            smth2[i] = dataArray[9];\n            phone_no1[i] = dataArray[10];\n            phone_no2[i] = dataArray[11];\n            email[i] = dataArray[12];\n            website[i] = dataArray[13];\n            customer_no[i] = dataArray[14];\n            }\n\n\n          //System.out.print(address[i] + "\\n"); \n          data = CSV.readLine(); // Read next line of data.\n          i++;\n      }\n
Run Code Online (Sandbox Code Playgroud)\n\n

先感谢您!

\n\n

一些数据是“E3B3C5EB-B101-4C43-8E0C-ADFE76FC87FE;“Var Welk”Inh.Kar;NULL;NULL;DE;16278;Rotr 3;Angerm\xc3\x83\xc2\xbcnde;NULL;NULL;03331/354348 -0;0343331/364548-15;info@aalls.com;http://www.adss.com;ipo241”,但可能有所不同(更小或更大)。

\n

小智 5

这应该可以解决问题:它基本上创建了 csv 文件的矩阵表示。

LinkedList<String[]> rows = new LinkedList<String[]>();
String dataRow = CSVFile.readLine();
// Read the number of the lines in .csv file 
// i = row of the .csv file
int i = 0; 
while ((datarow = CSVFile.readLine()) != null){
    i++;
    rows.addLast(dataRow.split(","));
}

String[][] csvMatrix = rows.toArray(new String[rows.size()][]);
Run Code Online (Sandbox Code Playgroud)

在 csvMatrix[行][列] 中...

访问列时,断言您尝试访问的列号在范围内,方法是:

if(col < csvMatrix[row].length)
Run Code Online (Sandbox Code Playgroud)