[英]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;
}
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.