Java-从CSV文件读取错误
因此,我的问题是,我试图从CSV文件读取和写入,但我遇到了一个问题,即附加到新行,然后读取,但没有得到以下错误: 现在我想我知道了为什么会出错,但我可以找到一种方法来解决它。我正在使用以下代码写入该文件:Java-从CSV文件读取错误,java,csv,bufferedreader,indexoutofboundsexception,filewriter,Java,Csv,Bufferedreader,Indexoutofboundsexception,Filewriter,因此,我的问题是,我试图从CSV文件读取和写入,但我遇到了一个问题,即附加到新行,然后读取,但没有得到以下错误: 现在我想我知道了为什么会出错,但我可以找到一种方法来解决它。我正在使用以下代码写入该文件: public void writeToFile(AbstractAccount account) { StringBuilder sb = new StringBuilder(); sb.append(account.getFirstname() + ","); s
public void writeToFile(AbstractAccount account)
{
StringBuilder sb = new StringBuilder();
sb.append(account.getFirstname() + ",");
sb.append(account.getLastname() + ",");
sb.append(account.getID() + ",");
sb.append(account.getPhonenum() + ",");
sb.append(account.getUsername() + ",");
sb.append(account.getPassword() + ",");
sb.append(account.getAccounttype());
FileWriter fw = null;
try
{
fw = new FileWriter("login.csv", true);
fw.append(System.lineSeparator() + sb.toString());
fw.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
问题是CSV文件的内部如下所示:
John,Doe,A1,0123456789,johnd,password1,Admin
Jane,Doe,A2,1234567890,janed,password2,CourseCoordinator
John,Smith,A3,2345678901,johns,password3,Approver
Jane,Smith,A4,356789012,johns,password4,CasualStaff
Test, Test, Test, Test, test, test, Admin
但当我将其复制到堆栈溢出文本编辑器中时,我可以看到实际格式如下:
John,Doe,A1,0123456789,johnd,password1,Admin
Jane,Doe,A2,1234567890,janed,password2,CourseCoordinator
John,Smith,A3,2345678901,johns,password3,Approver
Jane,Smith,A4,356789012,johns,password4,CasualStaff
Test, Test, Test, Test, test, test, Admin
因此,当我使用以下代码读取文件时:
public ArrayList<AbstractAccount> readFromFile(String filename)
{
BufferedReader br = null;
String line = "";
try
{
br = new BufferedReader(new FileReader(filename));
while ((line = br.readLine()) != null)
{
// use comma as separator
String[] attributes = line.split(",");
accounts.add(CreateAccount(attributes));
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return accounts;
}
不要手动解析CSV use(用于CSV解析的开源库)不要手动解析CSV use(用于CSV解析的开源库)您会收到异常,因为您发送到
CreateAccount
方法的数据(应该以小写字母开头)不是你期望的尺寸
例如,如果您发送了一个lenght=3的字符串数组,并尝试从数组中的第5位获取密码,则会得到ArrayIndexOutOfBoundsException
最可能发生的情况是,由于您的CSV解析逻辑,您向CreateCount方法发送了一个空数组
您可以编辑CreateCount代码,使其如下所示:
public AbstractAccount CreateAccount(String[] data)
{
if(data == null || data.length != 7){
// received data doesn't contain all the expected fields...
// maybe check the data before sending it to this method?
return null;
}
String firstname = data[0];
String lastname = data[1];
String id = data[2];
String phonenum = data[3];
String username = data[4];
String password = data[5];
String accounttype = data[6];
if (accounttype.equals("Admin"))
return new AdminModel(firstname, lastname, id, phonenum, username, password);
else if (accounttype.equals("CourseCoordinator"))
return new CourseCoordinatorModel(firstname, lastname, id, phonenum, username, password);
else if (accounttype.equals("Approver"))
return new ApproverModel(firstname, lastname, id, phonenum, username, password);
else if (accounttype.equals("CasualStaff"))
return new CasualStaffModel(firstname, lastname, id, phonenum, username, password);
else
return null;
}
希望它能有所帮助,同时,我建议您研究使用IDE的调试方法。它将在编码方面帮助您很多 之所以会出现异常,是因为发送到
CreateAccount
方法(应该以小写字母开头)的数据不是您期望的大小
例如,如果您发送了一个lenght=3的字符串数组,并尝试从数组中的第5位获取密码,则会得到ArrayIndexOutOfBoundsException
最可能发生的情况是,由于您的CSV解析逻辑,您向CreateCount方法发送了一个空数组
您可以编辑CreateCount代码,使其如下所示:
public AbstractAccount CreateAccount(String[] data)
{
if(data == null || data.length != 7){
// received data doesn't contain all the expected fields...
// maybe check the data before sending it to this method?
return null;
}
String firstname = data[0];
String lastname = data[1];
String id = data[2];
String phonenum = data[3];
String username = data[4];
String password = data[5];
String accounttype = data[6];
if (accounttype.equals("Admin"))
return new AdminModel(firstname, lastname, id, phonenum, username, password);
else if (accounttype.equals("CourseCoordinator"))
return new CourseCoordinatorModel(firstname, lastname, id, phonenum, username, password);
else if (accounttype.equals("Approver"))
return new ApproverModel(firstname, lastname, id, phonenum, username, password);
else if (accounttype.equals("CasualStaff"))
return new CasualStaffModel(firstname, lastname, id, phonenum, username, password);
else
return null;
}
希望它能有所帮助,同时,我建议您研究使用IDE的调试方法。它将在编码方面帮助您很多 能否显示
CreateAccount
的代码。添加了CreateCount代码后出现异常。能否显示CreateCount
的代码。在添加CreateCount代码时发生异常。