java - 为什么我不能用 Java 序列化它

标签 java serialization this

我很好奇为什么我无法将 this 成功写入 ObjectOutputStream

我编写了以下类,发现它可以很好地处理所有必要的导入:

public class Cereal implements Serializable{

    private String name ; 
    private FileInputStream fis ; 
    private ObjectInputStream ois ;
    private ObjectOutputStream oos; 
    private FileOutputStream fos ; 
    private Cereal readIn ; 
    private Cereal writeOut; 

    private Toy prize ; 
    public Cereal( String newName, String prizeName){
        name = newName ; 
    }
    public String readBox(){
        return name ; 
    }
    //Where it happens; unsuccessful line commented out
    public void writeToFile( String arg ) throws Exception{
        try{
            fos = new FileOutputStream(arg) ; 
            oos = new ObjectOutputStream(fos ) ; 
            writeOut = new Cereal(name, prize.describe());
            oos.writeObject(writeOut);
            //oos.writeObject( this );
            oos.close();
            fos.close(); 
            System.out.println("wrote to " + arg);

        }catch( Exception e ){
            System.out.println("Problem");
            e.printStackTrace();  
        }

    }
    public void writeToFile( ) throws Exception{
        //
        writeToFile( name + ".ser") ; 
    }
    public void readFromFile(String arg) throws Exception{

        try{
            fis = new FileInputStream(arg) ; 
            ois = new ObjectInputStream(fis) ;

            System.out.println("Read from " + arg); 
            readIn = (Cereal) ois.readObject() ; 
            this.name = readIn.readBox(); 
            ois.close(); 
            fis.close(); 

        }catch(Exception e){
            System.out.println("Had a problem");
            e.printStackTrace(); 
        }
    }
    public void readFromFile() throws Exception{
        readFromFile( name + ".ser"); 
    }


}

但用 oos.writeObject(this) 替换 oos.writeObject(writeOut) 时,我引发了以下错误:

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.io.FileOutputStream
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1355)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2000)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1924)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at Cereal.readFromFile(Cereal.java:56)
    at MainDriver.main(MainDriver.java:13)
Caused by: java.io.NotSerializableException: java.io.FileOutputStream
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at Cereal.writeToFile(Cereal.java:34)
    at Cereal.writeToFile(Cereal.java:47)
    at MainDriver.main(MainDriver.java:7)

我不确定为什么这会成为一个问题,有人知道吗?提前致谢。 (初始化属性似乎很麻烦,而不是像我现在所做的那样只使用 this。)

最佳答案

假设 Toy 也是 Serializable,您需要制作流 transient (因为它们不是 Serializable)。类似的东西,

private transient FileInputStream fis ; 
private transient ObjectInputStream ois ;
private transient ObjectOutputStream oos; 
private transient FileOutputStream fos ; 

这就是为什么你的堆栈跟踪的根说

Caused by: java.io.NotSerializableException: java.io.FileOutputStream

您还可以将这些流设为局部变量而不是字段,并使用 try-with-resources Close清理。有点像

public void readFromFile(String arg) throws Exception {
    try (ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream(arg))) {
        System.out.println("Read from " + arg);
        readIn = (Cereal) ois.readObject();
        this.name = readIn.readBox();
    } catch (Exception e) {
        System.out.println("Had a problem");
        e.printStackTrace();
    }
}

public void writeToFile(String arg) throws Exception {
    try (ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(arg))) {
        writeOut = new Cereal(name, prize.describe());
        oos.writeObject(writeOut);
        System.out.println("wrote to " + arg);
    } catch (Exception e) {
        System.out.println("Problem");
        e.printStackTrace();
    }
}

关于java - 为什么我不能用 Java 序列化它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36588711/

相关文章:

java - 在 Eclipse Oxygen 3 Release 中,该程序在最后一次迭代中不会调用循环,而其他 IDE 会调用循环

java - JSON 到 Java 中的对象?

javascript - 使用在 jQuery.on( 调用中对 'this' 进行操作的函数

Javascript 'this' 返回未定义的内部对象

javascript - 如何访问新创建的对象的变量?

java - 我怎样才能编写一个程序来总结txt文件中1-9、10-19等数字出现的次数?

Java将json字符串转为utf 8字符串

c# - Json.NET - 防止重新序列化已经序列化的属性

php - 无法在 php 中使用 unserialize() 函数并且无法显示数据

ruby-on-rails - 多态嵌套关联上的 Rails active_model_serializers