简体   繁体   English

如何在不复制代码的情况下从两个不同的CSV文件读取并创建两个不同的数组?

[英]How can I read from two different CSV files and create two different arrays without duplicating code?

This is my code. 这是我的代码。 Im reading data from two CSV files (one for users and one for restaurants) and Im supplying two different test classes in Selenium Webdriver with two different arrays in order to parametrize the tests. 我从两个CSV文件中读取数据(一个用于用户,一个用于餐厅),并且Im在Selenium Webdriver中提供了两个不同的测试类,它们具有两个不同的数组以对测试进行参数化。

Is there any way I dont repeat so much code here? 我有什么办法在这里不重复太多代码吗?

final String FILE_PATH = "src/test/resources/250.csv";
final String FILE_PATH2 = "src/test/resources/places.csv";

    //read CSV file and supply data for test purposes
    CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
    ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 5; i++) { // 5 is the number of columns
            list.add(nextLine[i]);
        }
        array.add(list);

    }
    reader.close();

    CSVReader reader2 = new CSVReader(new FileReader(FILE_PATH2));
    ArrayList<ArrayList<String>> array2 = new ArrayList<ArrayList<String>>();
    String[] nextLine2;
    while ((nextLine2 = reader2.readNext()) != null) {
        ArrayList<String> list2 = new ArrayList<String>();
        for (int i = 0; i < 5; i++) { // 5 is the number of columns
            list2.add(nextLine2[i]);
        }
        array2.add(list2);

    }
    reader2.close();

如何声明一个... 函数

ArrayList<ArrayList<String>> fromFileToArray(String filename)
  List<List<String>> getList(String filePath){
    CSVReader reader = new CSVReader(new FileReader(filePath));
    ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 5; i++) { // 5 is the number of columns
            list.add(nextLine2[i]);
        }
        array.add(list);

    }
    reader.close();

    return array;
  }
  • getList(FILE_PATH) getList(FILE_PATH)
  • getList(FILE_PATH2) getList(FILE_PATH2)

You create a method as below 您创建一个如下方法

ArrayList<ArrayList<String>> read (FileReader reader)
{
   CSVReader reader = new CSVReader(reader);
   ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
   String[] nextLine;
   while ((nextLine = reader.readNext()) != null) {
    ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < 5; i++) { // 5 is the number of columns
        list.add(nextLine[i]);
    }
    array.add(list);

}
reader.close();
return array;
}

Then you can call the method as below twice 然后您可以如下两次调用该方法

ArrayList<ArrayList<String>> array1 = read(new FileReader(FILE_PATH));
ArrayList<ArrayList<String>> array2 = read(new FileReader(FILE_PATH2));
public ArrayList<ArrayList<String>> getCSVContent(String filepath) throws Exception {
    CSVReader reader = new CSVReader(new FileReader(FILE_PATH));
    ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 5; i++) { // 5 is the number of columns
            list.add(nextLine[i]);
        }
        array.add(list);
    }
    reader.close();
    return array;
}

暂无
暂无

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

相关问题 如何在Spring Batch中从两个不同的文件夹中读取多个xml文件? - How can I read multiple xml files from two different folders in Spring Batch? 如何从两个不同的文件中读取文件,并将每个文件中的10个随机名称放到同一输出文件中,总共20个 - How can I read from two different files and put 10 random names from each for a total of 20 into the same output file 为什么我的代码在无限循环中运行? 将两个不同文件中的内容放入两个 arrays - why is my code running in an infinite loop? Putting something from two different files into two arrays 如何编写一个方法来散布两个不同长度的数组? - How can I write a method to intersperse two arrays of different lengths? 如何在Java中使用SequenceInputStream从两个不同的文件中读取两个对象 - how can I read two object from two different file, using SequenceInputStream in java 如何在不考虑行顺序的情况下区分两个不同的文本文件? - How can I diff two different text files without considering line order? 如何判断两个 class 文件为何/如何不同? - How can I tell why/how two class files are different? 如何连接两个不同的阵列? - How to connect two different arrays? 如何为两个不同的子类创建一个超类 - How can I create a SuperClass for two different SubClasses 可以从一个文件创建两个不同大小的数组/矩阵吗? - Possible to create two different sized arrays/matrices from a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM