从二进制文件到数组的对象读取过程中的ClassCastException

时间:2018-06-24 12:04:06

标签: java serialization classcastexception

我将类中的对象写到二进制文件中,现在我只想读回对象数组。但是我却出错了。

我的问题是我无法将文件读入对象“ aaa”。我想将其读取到名为“ aaa”的数组对象。以下是我得到的运行时异常。但是代码有意义,没有错误。 read.object返回一个对象类,我所做的全部都将其强制转换为(Customer [])。但是我却在下面看到了这个运行时错误。

enter image description here

这是我的代码

xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

我的人类课堂代码

         Customer[] customer = new Customer[100]; 
     Customer[] aaa = new Customer[100];
         try{

        file2 = new java.io.FileInputStream("Appointments.bin");
        object2 = new java.io.ObjectInputStream(file2);

        aaa = (Customer[]) object2.readObject(); <--- I think this is where the error occurs

        for(Customer ddd : aaa)
            System.out.print(ddd);


        object2.close();
        file2.close();

    } catch(java.io.FileNotFoundException a){

        System.out.print("Appointments.bin File Not Found , Please Contact The Developer to fix this issue. Thank you.\n\n");
        System.exit(-1);

    } catch(java.io.IOException b){

    } catch(java.lang.ClassNotFoundException c){

        System.out.print("dsada");
    }

}

我的客户分类代码

public abstract class Human implements java.io.Serializable{

protected String firstName , lastName;
protected java.util.Date dateCreated = new java.util.Date();

public Human(){
}

public Human(String firstName , String lastName){
    this.firstName = firstName;
    this.lastName = lastName;
}

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


public void setLastName(String lastName){
    this.lastName = lastName;
}

public String getFirstName(){
    return firstName;
}

public String getLastName(){
    return lastName;
}


public String getFullName(){
    return firstName + " " + lastName + "\n";
}

@Override
public String toString(){
    return "First Name = " + firstName + "\nLast Name = " + lastName + "\nDate Created = " + dateCreated.toString() + "\n";

}

public abstract String getID();

客户注册的完整代码。还没有完成,我只是围绕读写对象功能进行测试。所以是的,其中有些没有意义,而且是无组织的。

public class Customer extends Human implements java.io.Serializable {

private int code  = 1000;   
protected String customerID , day , time , service , carNo;


public Customer(){

}

public Customer(String firstName , String lastName , String service , String time , String day , String carNo){
     this.firstName = firstName;
     this.lastName = lastName;
     this.service = service;
     this.time = time;
     this.day = day;
     this.carNo = carNo;
     code++;
     customerID = "C" + code;
}

public String getService(){
    return service;
}

public String getTime(){
    return time;
}

public String getDay(){
    return day;
}

public String getCarNo(){
    return carNo;
}

@Override
public String getID(){
    return customerID;
}

public void setService(String service){
    this.service = service;
}

public void setTime(String time){
    this.time = time;
}

public void setDay(String day){
    this.day = day;
}

public void setCarNo(String carNo){
    this.carNo = carNo;
}

private void setID(String customerID){
   this.customerID = customerID;    
}

@Override
public String toString(){   
    return super.toString() + "CustomerID = " + customerID + "\n";

}    
}

1 个答案:

答案 0 :(得分:0)

您正在尝试将单个Customer对象投射为Customer个对象数组:aaa = (Customer[]) object2.readObject();

您可以从以下异常中读取: Assignment.Customer cannot be cast to [LAssignment.Customer。第一部分(Assignment.Customer)是虚拟机所拥有的东西,第二部分是它试图将其强制转换为([LAssignment.Customer)的东西。 [L部分是telling you it is an array of objects of a type的内部虚拟机方式。您不能将单个实例转换为数组。

Customer customer = (Customer)object2.readObject();可以在没有ClassCastException的情况下工作。

在我的示例中,我假设最后一个写入文件的客户是null引用。否则,您需要设计一种方法来知道何时停止阅读。

 List<Customer> customers = new ArrayList<>();
 try (ObjectInputStream object2 = new java.io.ObjectInputStream(new java.io.FileInputStream("Appointments.bin"));{
    Customer customer = (Customer)object2.readObject();

    while (customer != null) {
       customers.add(customer);
       customer = object2.readObject();
    }
 }
 customers.forEach(System.out::println);