简体   繁体   English

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

[英]Why can't I serialize this in Java

I am curious as to why I am unable to successfully write this to an ObjectOutputStream . 我很好奇,为什么我不能成功写入thisObjectOutputStream

I have written the following class and found it to work fine with all the necessary imports: 我写了下面的类,发现它可以正常工作所有必要的导入:

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"); 
    }


}

Yet replacing oos.writeObject(writeOut) with oos.writeObject(this) , I raise the following errors: 然后用oos.writeObject(writeOut) 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)

I am not sure why this should be an issue, anyone have an idea? 我不确定为什么这应该是一个问题,任何人都有想法? Thanks in advance. 提前致谢。 (It seems cumbersome to initialize an attribute rather than just use this as I am doing now.) (初始化属性似乎很麻烦,而不是像现在这样使用this 。)

Assuming Toy is also Serializable , you need to make your streams transient (because they aren't Serializable ). 假设Toy也是Serializable ,你需要使你的流transient (因为它们不是 Serializable )。 Something like, 就像是,

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

That is why the root of your stack trace says 这就是堆栈跟踪的根部所说的原因

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

You could also make those streams local variables instead of fields, and use try-with-resources Close to clean up. 您也可以使这些流本地变量而不是字段,并使用try-with-resources关闭以清理。 Something like 就像是

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();
    }
}

and

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM