Java的:读.csv文件并保存到阵列 [英] Java: Read .csv file and save into arrays

查看:250
本文介绍了Java的:读.csv文件并保存到阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异常的问题,而我想读一个.csv文件,每列存入数组。
虽然,它可能看起来长的节目,它是没有的。我只是有15个不同的阵列。

I have a problem with an exception while I am trying to read a .csv file and save each column into array. Although, it may seem long program, it isn't. I just have 15 different arrays.

这是例外异常线程mainjava.lang.ArrayIndexOutOfBoundsException:2的行

This is the exception "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2" in the row

部门由[i] = dataArray中[2];

department[i] = dataArray[2];

有什么我可以做什么?

      BufferedReader CSVFile = 
            new BufferedReader(new FileReader("Sub-Companies.csv"));

      String dataRow = CSVFile.readLine();
      // Read the number of the lines in .csv file 
      // i = row of the .csv file
      int i = 0; 
      while (dataRow != null){
          i++;
          dataRow = CSVFile.readLine();

        }
      System.out.println(i);
      // Close the file once all data has been read.
      CSVFile.close();

      // End the printout with a blank line.
      System.out.println();

      // Save into arrays
      customer_id = new String[i];
      company_name = new String[i];
      department = new String[i];
      employer = new String[i];
      country = new String[i];
      zipcode = new String[i];
      address = new String[i];
      city = new String[i];
      smth1 = new String[i];
      smth2 = new String[i];
      phone_no1 = new String[i];
      phone_no2 = new String[i];
      email = new String[i];
      website = new String[i];
      customer_no = new String[i];

      // Read first line.
      // The while checks to see if the data is null. If 
      // it is, we've hit the end of the file. If not, 
      // process the data.
      int j;
      int counter;
      i = 0;

      // Read the file again to save the data into arrays
      BufferedReader CSV = 
            new BufferedReader(new FileReader("Sub-Companies.csv"));

      String data = CSV.readLine();

      while (data != null){
          String[] dataArray = data.split(";");
          for (String item:dataArray) {
            customer_id[i] = dataArray[0];
            company_name[i] = dataArray[1];
            department[i] = dataArray[2];
            employer[i] = dataArray[3];
            country[i] = dataArray[4];
            zipcode[i] = dataArray[5];
            address[i] = dataArray[6];
            city[i] = dataArray[7];
            smth1[i] = dataArray[8];
            smth2[i] = dataArray[9];
            phone_no1[i] = dataArray[10];
            phone_no2[i] = dataArray[11];
            email[i] = dataArray[12];
            website[i] = dataArray[13];
            customer_no[i] = dataArray[14];
            }


          //System.out.print(address[i] + "\n"); 
          data = CSV.readLine(); // Read next line of data.
          i++;
      }

感谢你在前进!

有些数据是E3B3C5EB-B101-4C43-8E0C-ADFE76FC87FE;瓦尔·卫尔克INH嘉; NULL; NULL; DE; 16278; ROTR 3;Angermünde;NULL;NULL;03331/354348-0;0343331/364548-15;info@aalls.com;http://www.adss.com;ipo241\",但也可能不同(更小或更大)。

Some data is "E3B3C5EB-B101-4C43-8E0C-ADFE76FC87FE;"Var Welk" Inh. Kar;NULL;NULL;DE;16278;Rotr 3;Angermünde;NULL;NULL;03331/354348-0;0343331/364548-15;info@aalls.com;http://www.adss.com;ipo241", but there could differ (smaller or bigger).

推荐答案

最好是使用 ArraList<弦乐> ,如果你想转换为数组

Best is use ArraList<String> and if you want convert as Array.

你的问题是你指望没有台词的创建数组的大小,但要添加数据
根据拆分(;)所以在数组长度以及可用值不匹配,从劈数组中添加(;​​)。

your problem is you are counting no of lines to create array size but you are adding data based on split(";") so there is mismatch in array length and available values to add in array from split(";").

这篇关于Java的:读.csv文件并保存到阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆