线程“main”中的异常java.io.StreamCorruptedException:无效的类型代码:AC

时间:2015-03-27 00:15:45

标签: java exception eof objectinputstream eofexception

只是想读一个数字。我正在实施Serializable而我的main()会抛出错误:

线程“main”中的异常java.io.StreamCorruptedException:无效的类型代码:AC     at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1379)     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)     在Driver.main(Driver.java:28)

以下是代码:

try ( ObjectOutputStream output = new ObjectOutputStream(new 
           FileOutputStream("myFile.dat", true));){ 
           Integer a = 0;   
           output.writeObject(a); 
} 
finally{}; 
try(ObjectInputStream input = new ObjectInputStream(new 
          FileInputStream("myFile.dat")); ){ 
           Integer newNumber = 0; 
           newNumber =  input.readObject(); 
           ++newNumber; 
           System.out.println("This program has been executed " + newNumber
            + " times.");   
          input.close();
}

1 个答案:

答案 0 :(得分:1)

该文件为空,或者在标题后包含少于四个字节的实际数据(如果有)。