Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取和写入(序列化)不同类中的静态嵌套类对象_Java_Exception_Serialization_Core - Fatal编程技术网

Java 读取和写入(序列化)不同类中的静态嵌套类对象

Java 读取和写入(序列化)不同类中的静态嵌套类对象,java,exception,serialization,core,Java,Exception,Serialization,Core,我想序列化一个Map对象,该对象包含一个静态嵌套的CustomClass对象作为其值 public class A{ static Map<String, CustomClass> map = new HashMap<>(); public static void main(String[] args) { map.put("ABC", new CustomClass(1, 2L, "Hello")); writeToF

我想序列化一个Map对象,该对象包含一个静态嵌套的CustomClass对象作为其值

public class A{

    static Map<String, CustomClass> map = new HashMap<>();

    public static void main(String[] args) {
        map.put("ABC", new CustomClass(1, 2L, "Hello"));
        writeToFile();
    }

    private static void writeToFile() throws IOException, ClassNotFoundException {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.ser"));
        out.writeObject(map);
    }

    private static class CustomClass implements Serializable {
        int x;
        long y;
        String z;
        private static final long serialVersionUID = 87923834787L;
        private CustomClass (int x, long y, String z) {
         .....
        }
    }

}


public class B {

    static Map<String, CustomClass> map = new HashMap<>();

    public static void main( String[] args) {
        readFromFile();
    }

    private static void readFromFile() throws IOException, ClassNotFoundException {
        ObjectInputStream out = new ObjectInputStream(new FileInputStream("file.ser"));
        map = out.readObject(); // ClassNotFoundException occured
    }

    private static class CustomClass implements Serializable {
        int x;
        long y;
        String z;
        private static final long serialVersionUID = 87923834787L;
        private CustomClass (int x, long y, String z) {
         .....
        }

        //some utility methods
        ....
    }

}
公共A类{
静态映射映射=新的HashMap();
公共静态void main(字符串[]args){
map.put(“ABC”,新的CustomClass(1,2L,“Hello”);
writeToFile();
}
私有静态void writeToFile()引发IOException,ClassNotFoundException{
ObjectOutputStream out=新的ObjectOutputStream(新的FileOutputStream(“file.ser”));
out.writeObject(映射);
}
私有静态类CustomClass实现了可序列化{
int x;
长y;
字符串z;
私有静态最终长serialVersionUID=87923834787L;
私有自定义类(整数x,长y,字符串z){
.....
}
}
}
公共B级{
静态映射映射=新的HashMap();
公共静态void main(字符串[]args){
readFromFile();
}
私有静态void readFromFile()引发IOException,ClassNotFoundException{
ObjectInputStream out=新的ObjectInputStream(新的FileInputStream(“file.ser”));
map=out.readObject();//发生ClassNotFoundException
}
私有静态类CustomClass实现了可序列化{
int x;
长y;
字符串z;
私有静态最终长serialVersionUID=87923834787L;
私有自定义类(整数x,长y,字符串z){
.....
}
//一些实用方法
....
}
}
当我试图读取序列化映射对象时,它抛出ClassNotFoundException。是否因为在不同类下定义的相同嵌套类将具有不同的名称或版本

解决这个问题的可能办法是什么。
谢谢

嵌套类全名包括封闭类名称。A写入$CustomClass,B有B$CustomClass

嵌套类全名包括封闭类名称。A向A$CustomClass写入数据,B向B$CustomClass写入数据

是否因为在不同类下定义的相同内部类将具有不同的名称或版本

因为"不同阶级下定义的同一个内部阶级"在术语上是矛盾的。这是不一样的。这是一门不同的课

注意

是否因为在不同类下定义的相同内部类将具有不同的名称或版本

因为"不同阶级下定义的同一个内部阶级"在术语上是矛盾的。这是不一样的。这是一门不同的课


注意。

在您的
B类
中,我已替换为下面的一行,它对我很有效

 map = (Map<String, CustomClass>) out.readObject();//Line no 19
map=(map)out.readObject()//19号线

此外,我无法在
ObjectInputStream

中的
readObject(Object)
中找到一个将Object作为参数的方法,在您的
类B
中,我已经用下面的行替换了它,它对我来说运行良好

 map = (Map<String, CustomClass>) out.readObject();//Line no 19
map=(map)out.readObject()//19号线

另外,我在
ObjectInputStream

“readObject(Object)”中找不到使用
readObject(Object)
对象作为参数的方法。很抱歉,在这里键入演示代码时出错。请将其读为out.readObject(),则表示在运行
Class B
时未找到
Class A
本身。您可以在运行
Class B
之前检查类路径,或者在运行程序时使用SystemClassloader记录类路径。“readObject(Object)”很抱歉,在这里键入演示代码是一个错误。请将其读为out.readObject(),则表示在运行
Class B
时未找到
Class A
本身。您可以在运行
Class B
之前检查类路径,或者在运行程序时使用SystemClassloader记录类路径。