我不断收到 java.io.NotSerializableException: java.io.ObjectOutputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29592425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:16:38  来源:igfitidea点击:

I keep getting java.io.NotSerializableException: java.io.ObjectOutputStream

javaexceptionserializable

提问by Jeet Parekh

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.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)

I also tried using savobj.accData.writeObj(savobj)from main(), but I still get the same exception. What should I do?

我也尝试使用savobj.accData.writeObj(savobj)from main(),但我仍然遇到相同的异常。我该怎么办?

采纳答案by Luiggi Mendoza

Only primitives and classes that implement Serializableinterface can be serialized. ObjectOutputStreamdoesn't implement this interface.

只有实现Serializable接口的原语和类才能被序列化。ObjectOutputStream不实现这个接口。

Quick solution: use the ObjectOutputStreamin the narrowest possible scope, declare it in the method where it's being used, not as a field in the class. Do similar with other utility classes like Scanner.

快速解决方案:ObjectOutputStream在尽可能窄的范围内使用 ,在使用它的方法中声明它,而不是作为类中的字段。与其他实用程序类(如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 transientso 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);
}