嗨我正試圖反序列化以下對象。如何反序列化具有參數化構造函數的對象
public class Book extends Item implements Serializable {
public Boolean isBook;
public Boolean isBookch;
private String publicationPlace;
private String publisher;
private int edition;
private String pages;
private String article;
Book(String title, String author, String year)
{
super(title, author, year);
isBook = false;
isBookch = false;
publicationPlace = " !!! Not Known !!! ";
publisher = " !!! Not Known !!! ";
edition = 1;
pages = "!!! Not Known !!!";
article = " !!! Not Known !!! ";
}
這是對象的結構,我serailizing它....
openw("books.dat");
Date date = new Date();
out.writeObject(date);
for(Book b : books)
out.writeObject (b);
closew();
,這是工作非常精細的工作與出任何錯誤。 我試着分散它...
openr("books.dat");
Date lastsaved = (Date)in.readObject();
System.out.println("last saved date : " + lastsaved.toString());
while(true)
{
Object o = in.readObject();
if(o == null)
break;
else
{
addItem((Book)o);
books.add((Book)o);
}
}
closer();
,這是給錯誤如下:java.io.InvalidClassException:書;沒有有效的構造
如何清除這個問題.. thanx提前...
一個很好的解釋,請 http://stackoverflow.com/questions/7151711/java-why-doesnt-deserialization-invoke-constructor-whats-the-best-workaroun – Rahul
'的readObject()如果你寫了'null',則返回'null':否則。它在文件末尾拋出'EOFException',而不是返回null。當你閱讀一個null時幾乎肯定是不正確和不足的。 – EJP