繁体   English   中英

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

[英]Java: Read .csv file and save into arrays

我在尝试读取.csv文件并将每一列保存到数组时遇到异常问题。 尽管它看起来很长,但事实并非如此。 我只有15个不同的数组。

这是行中的异常“线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:2”

部门[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;“ 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”,但可能会有所不同(更大或更小)。

这应该可以解决问题:它基本上会创建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()][]);

在csvMatrix [row] [col] ...

访问列时,通过执行以下操作来断言您尝试访问的列号在范围内:

if(col < csvMatrix[row].length)

最好是使用ArraList<String> ,如果要convert as Array

您的问题是您没有计算创建数组大小的行数,而是基于split(“;”)添加数据,因此数组长度不匹配,并且无法从split(“;”)添加到数组中。

您的代码有几个问题。 例外是由于其中一行没有包含足够的';'而引起的 分隔值。

关于您的代码的奇怪之处是:

  for (String item:dataArray) {
    customer_id[i] = dataArray[0];

这只是意味着您重复相同的作业15次(只需删除for(字符串项目:...))。

如果我是你,请执行以下操作:

创建一个类; 像这样的东西:

public class Customer {
    private String customerId;
    private String companyName;

    // ...
    public static Customer create(final String... args) {
        if (args.length != 15) {
            return null; // or throw an exception
        }
        final Customer rv = new Customer();
        rv.setCustomerId(args[0]);
        rv.setCompanyName(args[1]);
        // ...
        return rv;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(final String customerId) {
        this.customerId = customerId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(final String companyName) {
        this.companyName = companyName;
    }
}

使用集合(如以上文章中所建议):

    BufferedReader csv = new BufferedReader(new FileReader("Sub-Companies.csv"));
    List<Customer> customers = new LinkedList<Customer>();

    String data;
    while ((data = csv.readLine()) != null){
        Customer customer = Customer.create(data.split(";"));
        if (customer != null) {
            customers.add(customer);
        }
    }

如果需要数组而不是集合,则可以执行以下操作:

Customer[] arr = customers.toArray(new Customer[customers.size()]);

使用库读取文件...例如,您可以尝试http://opencsv.sourceforge.net/

department[i] = dataArray[2];  

异常意味着dataArray没有那么多的元素(即3)。
如果您想解析CSV文件,可以通过指定缺少任何元素的占位符来简化生活。
我的意思是,您可以拥有类似以下的记录:

a;b;c;d;e;f;g;h;j
每个字符代表列的值, 缺少元素时,格式必须为:
a;;;;;f;g;h;j不是 a;f;g;h;j

这不是一个不寻常的期望,而是CSV文件中的规范,它将大大简化您的代码,并避免出现数组索引异常,因为您的行将始终具有预期的列

使用ArrayList:

public ArrayList<ArrayList<String>> parseDataFromCsvFile()
{
     ArrayList<ArrayList<String>> dataFromFile=new ArrayList<ArrayList<String>>();
     try{
         Scanner scanner=new Scanner(new FileReader("CSV_FILE_PATH"));
         scanner.useDelimiter(";");

         while(scanner.hasNext())
         {
            String dataInRow=scanner.nextLine();
            String []dataInRowArray=dataInRow.split(";");
            ArrayList<String> rowDataFromFile=new ArrayList<String>(Arrays.asList(dataInRowArray));
            dataFromFile.add(rowDataFromFile);
         }
         scanner.close();
     }catch (FileNotFoundException e){
        e.printStackTrace();
     }
     return dataFromFile;
}

调用方法(显示csv内容):

ArrayList<ArrayList<String>> csvFileData=parseDataFromCsvFile();

public void printCsvFileContent(ArrayList<ArrayList<String>> csvFileData)
{
    for(ArrayList<String> rowInFile:csvFileData)
    {
        System.out.println(rowInFile);
    }
}

如果要使用Gradle(而不是Maven)将数据加载到Parameterized JUnit测试中,请使用以下方法:

// import au.com.bytecode.opencsv.CSVReader;
@Parameters(name = "{0}: {1}: {2}")
public static Iterable<String[]> loadTestsFromFile2() {
    String separator = System.getProperty("file.separator");
    File tFile = loadGradleResource( System.getProperty("user.dir") + 
        separator +  "build" + separator + "resources" + separator +  "test" + 
            separator + "testdata2.csv" );
    List<String[]> rows = null;
    if ( tFile.exists() ) {
        CSVReader reader = null;
        try {
            reader = new CSVReader( new FileReader( tFile ), ',' );
            rows = reader.readAll();
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }   
    }
    staticlogger.info("Finished loadTestsFromFile2()");
    return rows;
} 

请检查java.util.StringTokenizer帮助

例:

StringTokenizer tokenizer = new StringTokenizer(inputString, ";")

手册: StringTokenizer文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM