我很好奇为什么我无法成功地将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(writeOut)
替换为oos.writeObject(this)
,我提出以下错误:
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
。)
答案 0 :(得分:3)
假设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();
}
}