[英]I keep getting java.io.NotSerializableException: java.io.ObjectOutputStream
This is the code that I have been trying 这是我一直在尝试的代码
import java.util.Scanner;
import java.io.*;
abstract class Account implements Serializable {
protected String accountHolderName;
protected long balance;
protected ObjectOutputStream accData;
Scanner input = new Scanner(System.in);
}
class Savings extends Account implements Serializable {
Savings() throws IOException {
System.out.print("enter your name: ");
accountHolderName = input.nextLine();
System.out.print("\n");
System.out.print("enter your balance: ");
balance = input.nextLong();
accData = new ObjectOutputStream(new FileOutputStream(accountHolderName + ".bin"));
accData.writeObject(this);
accData.close();
}
}
class Banking implements Serializable {
public static void main(String args[]) throws IOException {
Scanner input = new Scanner(System.in);
Savings savobj = new Savings();
}
}
and this is the exception I get 这是我得到的例外
Exception in thread "main" java.io.NotSerializableException: java.io.ObjectOutputStream at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) at java.io.ObjectOutputStream.writeSerialData(Unknown Source) at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at Savings.(Banking.java:22) at Banking.main(Banking.java:30)
线程“main”java.io.NotSerializableException中的异常:java.io.ObjectOutputStream.writeSod上的java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)中java.io.ObjectOutputStream.writeObject0(未知源)的java.io.ObjectOutputStream(未知源)位于java.io.ObjectOutputStream.writeObject0(未知来源)的java.io.ObjectOutputStream.writeOrdinaryObject(未知来源)位于java.io.ObjectOutputStream.writeObject(未知来源),来自储蓄。(Banking.java:22)at at Banking.main(Banking.java:30)
I also tried using savobj.accData.writeObj(savobj)
from main(
), but I still get the same exception. 我也尝试从
main(
)使用savobj.accData.writeObj(savobj)
,但我仍然得到相同的异常。 What should I do? 我该怎么办?
Only primitives and classes that implement Serializable
interface can be serialized. 只能实现实现
Serializable
接口的基元和类。 ObjectOutputStream
doesn't implement this interface. ObjectOutputStream
未实现此接口。
Quick solution: use the ObjectOutputStream
in the narrowest possible scope, declare it in the method where it's being used, not as a field in the class. 快速解决方案:在尽可能窄的范围内使用
ObjectOutputStream
,在使用它的方法中声明它,而不是在类中的字段中声明它。 Do similar with other utility classes like Scanner
. 与
Scanner
等其他实用程序类相似。
abstract class Account implements Serializable {
protected String accountHolderName;
protected long balance;
//protected ObjectOutputStream accData;
//Scanner input = new Scanner(System.in);
}
class Savings extends Account implements Serializable {
Savings() throws IOException {
Scanner input = new Scanner(System.in);
System.out.print("enter your name: ");
accountHolderName = input.nextLine();
System.out.print("\n");
System.out.print("enter your balance: ");
balance = input.nextLong();
ObjectOutputStream accData = new ObjectOutputStream(new FileOutputStream(accountHolderName + ".bin"));
accData.writeObject(this);
accData.close();
}
}
Another solution may be just marking these fields as transient
so they won't be serialized/deserialized: 另一个解决方案可能只是将这些字段标记为
transient
因此它们不会被序列化/反序列化:
abstract class Account implements Serializable {
protected String accountHolderName;
protected long balance;
protected transient ObjectOutputStream accData;
transient Scanner input = new Scanner(System.in);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.