Java在读取流时引发InvalidClassException
我想使用FileInputStream和ObjectInputStream读取文件。我已经创建了BankAccount类,它实现了Externalizable接口并@override了它的两个方法。我不明白它为什么抛出IOException 以下是接口外部化的重写方法:Java在读取流时引发InvalidClassException,java,serialization,ioexception,objectinputstream,objectoutputstream,Java,Serialization,Ioexception,Objectinputstream,Objectoutputstream,我想使用FileInputStream和ObjectInputStream读取文件。我已经创建了BankAccount类,它实现了Externalizable接口并@override了它的两个方法。我不明白它为什么抛出IOException 以下是接口外部化的重写方法: @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { char a = in.re
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
char a = in.readChar();
id = in.read();
username = (String)in.readObject();
name = (String)in.readObject();
password = (String)in.readObject();
amount = in.readDouble();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeChar('A');
out.write(id);
out.writeObject(username);
out.writeObject(name);
out.writeObject(password);
out.writeDouble(0);
}
这是我在main类中调用这些方法的代码:
try
{
// Writing the object into file
FileOutputStream file = new FileOutputStream(path);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(account);
out.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
try {
//Reading object from file
FileInputStream file = new FileInputStream(path);
ObjectInputStream out = new ObjectInputStream(file);
BankAccount bankk = (BankAccount)out.readObject();
System.out.println(bankk);
out.close();
file.close();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
这就是它扔的东西:
java.io.InvalidClassException: al.tct.bank_project.AdminAccount; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:157)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:862)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2038)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1568)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:428)
at al.tct.bank_project.BankApp$RegisterStage$1.writeToFile(BankApp.java:463)
at al.tct.bank_project.BankApp$RegisterStage$1.handle(BankApp.java:412)
at al.tct.bank_project.BankApp$RegisterStage$1.handle(BankApp.java:404)
堆栈跟踪至少包含4个有趣的信息位: [1] 类型。IOException有子类。也许这是FileNotFoundException [2] 堆栈跟踪。这是一条线,指向源文件中发生错误的位置,以及导致执行此方法的“调用链” [3] 信息。异常可以有一条消息,用可读的英语解释更多内容 [4] 因果链。异常可能有原因,通常异常本身很有趣(原因也可能有原因) 还有更多(例如抑制链) 如果你捕捉到一个异常并试图打印它,你就把事情搞砸了,,打印所有这些都是很难做到的,但这一切都很重要 因此,不要捕获异常,除非您能够实际处理它。打印错误消息或记录错误消息时,无法处理它 在这里,只需将您的方法声明为“抛出IOException”。如果这是不可能的(仅当您从超类/接口扩展/实现不允许您这样做的方法时才会发生),那么“我不知道该怎么做”代码如下:
catch (ExceptionICannotHandle e) {
throw new RuntimeException(e);
}
这是保证你得到所有信息的唯一方法
我建议你更新你的代码,然后你就会清楚这个错误的原因。你能展示一下
IOException
stacktraces吗?请在你的问题中显示Stack traces。不要调用输入流out
ex.printStackTrace()
总是(除非它被记录)我发布了IOException stacktraces。。。但我仍然不明白这是一个InvalidClassException
,它的错误消息准确地说明了问题所在。