I want to read from a file using FileInputStream and ObjectInputStream. I have created BankAccount class which implements Externalizable interface and @overriden both its methods. I do not understand why it throws an IOException.
These are the overriden methods of interface Externalizable:
@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);
}
This is the code where i call these methods in main class:
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");
}
This is what it threw:
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)