我在尝试读取.csv文件并将每列保存到数组时遇到异常问题。 虽然,它可能看起来很长,但事实并非如此。我只有15个不同的阵列。
这是异常“线程中的异常”主“java.lang.ArrayIndexOutOfBoundsException: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;”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“,但可能有所不同(更小或更大)。
答案 0 :(得分:4)
这应该可以解决问题:它基本上创建了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)
答案 1 :(得分:2)
最好使用ArraList<String>
,如果您需要convert as Array
。
你的问题是你在计算没有行来创建数组大小,但是你正在添加数据 基于split(“;”),因此数组长度不匹配,可用值从split(“;”)添加到数组中。
答案 2 :(得分:2)
您的代码存在一些问题。这个例外的原因是其中一行不包含足够的';'分开的值。
关于你的代码的奇怪之处在于:
for (String item:dataArray) {
customer_id[i] = dataArray[0];
这只是意味着你重复相同的作业15次(只需删除for(String item:...))。
如果我是你,我会做以下事情:
创建一个类;像这样的东西:
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/。
答案 3 :(得分:1)
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文件中的标准,会大大简化您的代码并避免数组索引异常,因为您的行总是有预期的列
答案 4 :(得分:1)
使用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);
}
}
答案 5 :(得分:0)
如果要使用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;
}
答案 6 :(得分:0)
请检查java.util.StringTokenizer
是否有帮助
示例:
StringTokenizer tokenizer = new StringTokenizer(inputString, ";")