简体   繁体   中英

Reading Ser file (Serialization)

Can someone help me, to read the values from the ser file, and store the objects (customer and supplier) in seperate arraylist. I've been trying no luck.

public void writeToFile(){
        try{
           output.writeObject(new Customer("C150", "Luke", "Atmyass", "Bellville", "1981-01-27", 1520.50, false));
           output.writeObject(new Supplier("S270", "Grand Theft Auto", "Toyota", "Mid-size sedan"));
           output.writeObject(new Customer("C130", "Stu", "Padassol", "Sea Point", "1987-05-18", 645.25, true));
           output.writeObject(new Supplier("S400", "Prime Motors", "Lexus", "Luxury sedan"));
           output.writeObject(new Supplier("S300", "We got Cars", "Toyota", "10-seater minibus"));
           output.writeObject(new Customer("C100", "Mike", "Rohsopht", "Bellville", "1993-01-24", 975.10, true));
           output.writeObject(new Customer("C300", "Ivana.B", "Withew", "Langa", "1998-07-16", 1190.50, false));
           output.writeObject(new Supplier("S350", "Auto Delight", "BMW", "Luxury SUV"));
           output.writeObject(new Supplier("S290", "MotorMania", "Hyundai", "compact budget"));
           output.writeObject(new Customer("C250", "Eileen", "Sideways", "Nyanga", "1999-11-27", 190.85, true));
           output.writeObject(new Customer("C260", "Ima", "Stewpidas", "Atlantis", "2001-01-27", 1890.70, true));
       }//end try

Here is the implementation of Stakeholder class:

import java.io.Serializable; 
     
public class Stakeholder implements Serializable { 
    private String stHolderId; 
    
    public Stakeholder() { } 

    public Stakeholder(String stHolderId) { this.stHolderId = stHolderId; } 
    public String getStHolderId() { return stHolderId; }
    public void setStHolderId(String stHolderId) { this.stHolderId = stHolderId; } 

    @Override public String toString() { return stHolderId; } 
}

The two object inherit from Stakeholder class

Here is the implementation of Customer class:

public class Customer extends Stakeholder{
private String firstName;
private String surName;
private String address;
private String dateOfBirth;
private double credit;
private boolean canRent;   //true - can rent a car; false - not allowed to rent because they have not returned a previously rented car

public Customer()    {
    super();
}

public Customer(String stHolderId, String firstName, String surName, String addr, String dob, double cred, boolean canRent)    {
    super(stHolderId);
    setFirstName(firstName);
    setSurName(surName);
    setAddress(addr);
    setDateOfBirth(dob);
    setCredit(cred);
    setCanRent(canRent);
}

public void setFirstName(String firstName)
{
    this.firstName = firstName;
}

public void setSurName(String surName)   {
    this.surName = surName;
}

public void setAddress(String addr)    {
    this.address = addr;
}

public void setDateOfBirth(String dob) { this.dateOfBirth = dob; }

public void setCredit(double cred)    {
    this.credit = cred;
}

public void setCanRent(boolean canRent)  {
    this.canRent = canRent;
}

public String getFirstName()     {
    return firstName;
}

public String getSurName()    {
    return surName;
}

public String getAddress()    {
    return address;
}

public String getDateOfBirth() { return dateOfBirth; }

public double getCredit()    {
    return credit;
}

public boolean getCanRent()     {
    return canRent;
}

@Override
public String toString()      {
    return String.format("%-10s\t%-10s\t%-10s\t%-15s\t%-10s\tR%-10.2f\t%-10s", super.toString(), getFirstName(), getSurName(),
            getAddress(), getDateOfBirth(), getCredit(), getCanRent());
}

}

Here is the implementation of Supplier class:

public class Supplier extends Stakeholder{
private String name;
private String productType;
private String productDescription;

public Supplier() {
    super();
}

public Supplier(String stHolderId, String name, String productType, String productDescription ) {
    super(stHolderId);
    this.name = name;
    this.productType = productType;
    this.productDescription = productDescription;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getProductType() {
    return productType;
}

public void setProductType(String productType) {
    this.productType = productType;
}

public String getProductDescription() {
    return productDescription;
}

public void setProductDescription(String productDescription) {
    this.productDescription = productDescription;
}

@Override
public String toString() {
    return String.format("%-5s\t%-20s\t%-10s\t%-15s",super.toString(), getName(),getProductType(), getProductDescription());
}

}

I think, it should be relatively easy. You have an ObjectInputStream and read one object at a time. Then you decide by the object type where to put it.

    List<Supplier> suppliers=new ArrayList<>();
    List<Consumer> consumers=new ArrayList<>();
    try(ObjectInputStream in=new ObjectInputStream(...))
    {
        for(Object o=in.readObject();o!=null;o=in.readObject())
        {
            if(o instanceof Supplier)
                suppliers.add((Supplier)o);
            else if(o instanceof Consumer)
                consumers.add((Consumer)o);
            else
                System.err.println("unknown object type: "+o);
        }
    }
    catch(Exception ex)
    {
        System.err.println(ex);
    }

  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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