如何修复此错误java.io.EOFException

时间:2018-11-24 21:41:15

标签: objectinputstream

如果电子邮件已经存在,我想读取文件,但是如果不将其写入文件,则什么也不做。

private void readEmail(String file, Email emails) {
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        Email email;
        while ((email = (Email) objectInputStream.readObject()) == null || (email = (Email) objectInputStream.readObject()) != null) {
            if(!emails.getEmail().equals(email)){
                writeInFile(file, emails);
            }else{
                System.out.println("User already exist!");
            }
        }
        objectInputStream.close();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案