package com.hct.Job;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestCSV {
//讀取CSV文件的靜態(tài)方法,使用CSV文件的文件路徑作為函數(shù)參數(shù)
public static Object[][] getSearchData(String FileName) throws IOException{
List<Object[]> records=new ArrayList<Object[]>();
String record;
//設(shè)定UTF-8字符集,使用帶緩沖區(qū)的字符輸入流BufferedReader讀取文件內(nèi)容
BufferedReader file=new BufferedReader(new InputStreamReader(new FileInputStream(FileName),"UTF-8"));
//忽略讀取CSV文件的標題行(第一行)
file.readLine();
//遍歷讀取文件中除第一行外的其他所有內(nèi)容并存儲在名為records的ArrayList中,每一行records中存儲的對象為一個String數(shù)組
while((record=file.readLine())!=null){
String fields[]=record.split(",");
// System.out.println(fields);
records.add(fields);
}
//關(guān)閉文件對象
file.close();
//將存儲測試數(shù)據(jù)的List轉(zhuǎn)換為一個Object的二維數(shù)組
Object[][] results=new Object[records.size()][];
//設(shè)置二位數(shù)組每行的值,每行是一個Object對象
for(int i=0;i<records.size();i++){
results[i]=records.get(i);
}
return results;
}
@DataProvider(name="searchData")
public static Object[][] data() throws IOException
{
String filename ="D:\eclipse-workspace\Job\src\test\java\com\hct\Job\demo.csv";
return getSearchData(filename);//獲取CSV文件的測試數(shù)據(jù)
}
@Test(dataProvider="searchData")
public void testData(String n1,String n2,String n3){
System.out.println(n1+"+++++++++++++++++++++++++++++"+n2+"+++++++++++++++++"+n3);
}
}