【问题标题】:ClassCastException when reading from binary file从二进制文件读取时出现 ClassCastException
【发布时间】:2013-12-04 21:20:45
【问题描述】:

我在使用通用方法读取二进制文件时收到java.lang.ClassCastException。 我试图用相同的方法读取两个不同的文件,但是我得到了异常,我不知道为什么。

这是读取文件的通用方法中的代码: ReadReadBenary 类的方法部分。

 import java.io.ObjectInputStream;
 import java.io.FileInputStream;
 import java.io.IOException;

 public class ReadBinary{

private ObjectInputStream reader;

public boolean open(String name) {
    try {
        reader= new ObjectInputStream(new FileInputStream(name));
        return true;
    } catch (IOException exception) {
        System.err.println("Error");
        return false;
    }
}

public void close() {
    try {
        if (reader != null) {
            lector.close();
        }
    } catch (IOException exception) {
        System.err.println("Error");
    }
}

public <E> E read() {

    E object = null;
    try {
        object = (E) reader.readObject();
    } catch (ClassNotFoundException ex) {
        System.err.println("Class dont found");
        System.err.println(ex);
    } catch (IOException ex) {
        System.err.println("End of the file");
        System.err.println(ex);
    } catch (ClassCastException ex) {
        System.err.println("Class dont found");
    }
    return object;
}

这是main中的代码:

 ReadBinary reader= new ReadBinary();

      if (reader.open(file3)) {
      *  Bill bill = reader.read();
        while (bill != null) {
            manejadorBill.insert(bill);
            bill = reader.read();
        }
        reader.close();
       }

    if (reader.open(file1)) {
        Customer customer= reader.read();
        while (customer != null) {
            manejadorCustomer.insert(customer);
            customer = reader.read();
        }
        reader.close();
    }

这是将对象写入文件的方法:

public <E> void escribir(E objeto) {
    try {
        escritor.writeObject(objeto);
    } catch (IOException exception) {
        System.err.print("Error rawr");
    }
}

BillCustomer 是两个独立的类。 当我尝试运行程序时,它说Customer 不能转换为Bill

它表示错误发生在带 * 的行中。

【问题讨论】:

  • 什么是ReadBinary?它是如何读取文件的? Bill 在文件中是如何表示的?
  • 您需要告诉我们这些类是什么,以及它们在层次结构中的位置。
  • 您需要发布异常。它会准确地告诉您对象的真正类别。

标签: java serialization classcastexception randomaccessfile


【解决方案1】:

您的file3 中似乎没有Bill 对象。 要检查此更改 read() 为:

public <E> E read() {
    Objject obj;
    E object = null;
    try {
          obj = reader.readObject();
          System.out.println(obj.getClass().getName())       
          object = (E) obj;
    } catch (ClassNotFoundException ex) {
        System.err.println("Class dont found");
        System.err.println(ex);
    } catch (IOException ex) {
        System.err.println("End of the file");
        System.err.println(ex);
    } catch (ClassCastException ex) {
        System.err.println("Class dont found");
    }
    return object;
}

看看什么是实际类型。

【讨论】:

    【解决方案2】:

    ClassCastException 表示写入流的下一个对象实际上不是Bill。检查编写对象的代码,看看它实际上正在编写什么类型的对象。一些 System.out.printlns 可能有助于准确查看正在写入的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多