Java - 从CSV文件读取错误

时间:2018-05-05 04:44:34

标签: java csv bufferedreader indexoutofboundsexception filewriter

所以我的问题是我试图从CSV文件中读取和写入,但是我遇到了追加新行然后阅读而没有收到以下错误的问题:

enter image description here

现在我想我知道为什么我会收到错误,但我可以找到解决问题的方法。我使用以下代码写入文件:

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;
}

我得到了之前提到过的错误。我已经尝试了几种写入新行的方法,但我找不到一个不能给我那个空白行的方法。有人有任何想法吗?

编辑:创建帐户代码

public AbstractAccount CreateAccount(String[] data)
{
    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;
}

2 个答案:

答案 0 :(得分:2)

不要手动解析CSV使用OpenCSV这是一个用于CSV解析的开源库

答案 1 :(得分:2)

您收到异常,因为您发送到CreateAccount方法的数据(应以小写字母开头)不是您期望的大小。

例如,如果您发送了一个lenght = 3的String数组,并且您尝试从数组中的第5位获取密码,则会得到ArrayIndexOutOfBoundsException

最有可能发生的事情是,由于您的CSV解析逻辑,您将一个空数组发送到CreateAccount方法。

您可以将CreateAccount代码编辑为如下所示:

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的调试方法。它会对编码有很大的帮助!