-1
Java對象串行:我序列化這種類型的哈希表的例如(整數,僱員)的並且當我反序列化這一點,我想將輸出在HashMap中。這可能嗎?因爲我得到了java.lang.ClassCastExceptionJava對象序列:序列化一個Hashtable,然後反序列化在一個HashMap
public class Employee implements Serializable {
private static final long serialVersionUID = -7260877684654746408L;
private String name;
private int age;
Employee(String n, int a) {
name=n;
age=a;
}
public String toString() {
return "Name: "+name+". "+"Age: "+age+".";
}
}
public class Test {
public static void main(String[] args) {
Hashtable<Integer, Employee> ht = new Hashtable<Integer, Employee>() {
{
put(1, new Employee("John", 37));
put(2, new Employee("Julia", 36));
}
};
//HashMap<Integer,Employee> hm = new HashMap<Integer,Employee>();
try {
FileOutputStream outSer = new FileOutputStream("outSer.ser");
ObjectOutputStream os = new ObjectOutputStream(outSer);
os.writeObject(ht);
os.close();
FileInputStream input = new FileInputStream("outSer.ser");
ObjectInputStream ois = new ObjectInputStream(input);
HashMap<Integer,Employee> hm= (HashMap<Integer, Employee>)ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
顯示你的努力 – sForSujit
顯示僱員和Person類太 – sForSujit
@sForSujit爲什麼?他們不會改變答案。 – EJP