簡體   English   中英

我嘗試序列化不可序列化的對象列表不起作用

[英]My attempt to serialize a non-serializable object list is not working

我有一個可序列化的類,其中包含一個不可序列化對象的列表。 所以我將該列表標記為瞬態並在我的類中實現了readObjectwriteObject 但是當我序列化和反序列化我的對象時,沒有輸入readObjectwriteObject方法(我已經通過使用 sout 證明了這一點)。 不知道為什么它不起作用。 這是我的代碼。

我的可序列化類:

public class Product implements Serializable
{
private String reference;
private String name;
private int stock;
private double defaultPrice;
private transient List<Sale> sales = new ArrayList<>();

public Product(String reference, String name, int stock, double defaultPrice) 
{
    this.reference = reference;
    this.name = name;
    this.stock = stock;
    this.defaultPrice = defaultPrice;

}

public Product() {}

public void addSale(Date from, Date to, int minQuantity, int maxQuantity, double specialPrice)
{
    sales.add(new Sale(from, to, minQuantity, maxQuantity, specialPrice));
}

/*public double findPrice(Date date, int quantity)
{

}*/

public List<Sale> getSales()
{
    return sales;
}

 void writeObject(ObjectOutputStream oos) throws IOException
{
    System.out.println("ENTRO AQUI");
    oos.defaultWriteObject();
    oos.writeInt( sales.size() );
    for(Sale sale: this.sales)
    {

       oos.writeObject( sale.getFrom());
       oos.writeObject( sale.getTo() );
       oos.writeInt( sale.getMinQuantity() );
       oos.writeInt( sale.getMaxQuantity() );
       oos.writeDouble( sale.getSpecialPrice() );
    }
}

public void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException
{
    ois.defaultReadObject();
    int salesSize = ois.readInt();
    for(int i = 0; i < salesSize; ++i)
    {
        Date from = (Date) ois.readObject();
        Date to = (Date) ois.readObject();;
        int minQuantity = (int) ois.readObject();
        int maxQuantity = (int) ois.readObject();
        double specialPrice = (double) ois.readObject();
        Sale sale = new Sale(from, to, minQuantity, maxQuantity, specialPrice);
        this.sales.add(sale);
    }

}

我沒有發布 setter 和 getter,因為我認為它們不相關。

我的不可序列化類:

public class Sale 
{
private Date from;
private Date to;
private int minQuantity;
private int maxQuantity;
private double specialPrice;

public Sale(Date from, Date to, int minQuantity, int maxQuantity, double specialPrice) 
{
    this.from = from;
    this.to = to;
    this.minQuantity = minQuantity;
    this.maxQuantity = maxQuantity;
    this.specialPrice = specialPrice;
}

public Sale() {
}

順便說一下,這是我的讀者:

public class BusinessBinaryReader 
{
public Business read(File file) throws IOException, ClassNotFoundException
{
    try(FileInputStream fis = new FileInputStream( file );
        BufferedInputStream bis = new BufferedInputStream( fis );
        ObjectInputStream ois = new ObjectInputStream( bis )
        )
    {

        return (Business) ois.readObject();
    }
}
}

還有我的作家:

public class BusinessBinaryWriter 
{
public void write(File file, Business business) throws IOException
{
    try(FileOutputStream fos = new FileOutputStream( file );
        BufferedOutputStream bos = new BufferedOutputStream( fos );
        ObjectOutputStream oos = new ObjectOutputStream( bos )
        )
    {
        oos.writeObject(business);
        oos.flush();
    }
}

}

您的自定義序列化函數readObjectwriteObject具有錯誤的簽名。 嘗試將它們設置為私有,它應該可以工作。

文檔

在序列化和反序列化過程中需要特殊處理的類必須實現具有以下確切簽名的特殊方法:

private void writeObject(java.io.ObjectOutputStream out)
 throws IOException

private void readObject(java.io.ObjectInputStream in)
 throws IOException, ClassNotFoundException;

作為旁注,確實鼓勵定義 SerialVersionUID,如果您的序列化對象長期存在,則更是如此。 在這個答案中解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM