【问题标题】:Deserializing throws java.lang.ClassCastException反序列化抛出 java.lang.ClassCastException
【发布时间】:2020-02-07 22:53:27
【问题描述】:

我在安卓设备上保存了一个序列化的类。将其传输到 win 10 PC 并通过以下方式加载文件:

fis = new FileInputStream(result.get(i));
ois = new ObjectInputStream(fis);
Object obj = ois.readObject();

Android 和 win 10 上的课程是:

public class ImgLogFile implements Serializable{

        byte[] frame;
        byte[] result;
        String config;

    public String getConfig(){
            return config;
        }

    public byte[] getFrame() {
        return frame;
    }

    public byte[] getResult() {
        return result;
    }
} 

当我尝试将加载的对象转换为它的类时,我得到:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class [Lde.mtt.smartiePlatform.ImgLogFile; cannot be cast to class de.mtt.smartiePlatform.ImgLogFile ([Lde.mtt.smartiePlatform.ImgLogFile; and de.mtt.smartiePlatform.ImgLogFile are in unnamed module of loader 'app')

我注意到一条路径前面的“L”,但不知道它是什么意思。 我怎样才能解决这个问题 ?

【问题讨论】:

    标签: java android serialization deserialization classcastexception


    【解决方案1】:

    [...] 类 [Lde.[...] 不能转换为类 de.[...]

    [ 表示一个数组。 [L 是后面引用类型的数组。

    所以你已经序列化了一个数组,并试图将反序列化的对象转换为不是数组的类型。

    (另外,使用 Java 序列化也不是一个好主意。)

    【讨论】:

    • 你建议改用什么?
    • @Niro 某种形式的 JSON 库似乎很常见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多