序列化 - 不工作

时间:2015-06-11 12:20:20

标签: java serialization

我使用类似How to serialize an object into a string的方法创建类 并且每次都说错误“java.lang.ClassCastException:java.lang.String不能转换为Myclass”

我的代码: 1)

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.xml.bind.DatatypeConverter;

public class Serialization {

    public static Object fromString(String s) throws IOException,
            ClassNotFoundException {
        byte[] data = DatatypeConverter.parseBase64Binary(s);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
                data));
        Object o = ois.readObject();
        ois.close();
        return o;
    }

    public static String toString(Serializable o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();
        return DatatypeConverter.printBase64Binary(baos.toByteArray());
    }
}

2) - 致电

MyClass hl = (MyClass) Serialization.fromString(items
        .getString("data"));
hl.load(); // this is my method from class

3)MyClass - 全息图

public class Hologram implements Serializable {

 /**
 * 
 */
private static final long serialVersionUID = 1L;

private Location loc;
private String name;
private String displayname;
public ArmorStand stand;

public Hologram(String name, String displayname, Location loc) {
this.loc = loc;
this.name = name;
this.displayname = displayname;

ArmorStand as = (ArmorStand) loc.getWorld().spawnEntity(loc,
    EntityType.ARMOR_STAND);

as.setGravity(false);
as.setCanPickupItems(false);
as.setCustomName(displayname);
as.setCustomNameVisible(true);
as.setVisible(false);

this.stand = as;

HologramManager.holograms.put(name, this);

}

public void move(Location loc) {
this.loc = loc;

stand.teleport(loc);
}

public Location getLocation() {
return this.loc;
}

public void remove() {
stand.remove();
HologramManager.holograms.remove(name);
}

public void removeHologram() {
HologramManager.remove(name);
}

public void changeName(String name) {
HologramManager.holograms.remove(this.name);
this.name = name;
HologramManager.holograms.put(name, this);
}

public void changeDisplayName(String displayName) {
this.displayname = displayName;
stand.setCustomName(displayname);
stand.setCustomNameVisible(true);
}

public void load() {
//todo
}

}

2 个答案:

答案 0 :(得分:1)

根据链接的答案,问题很可能在于您向我们展示的代码。序列化MyClass对象时,您可能正在执行以下操作:

MyClass hl;
String base64String = Serialization.toString(hl.toString());

但是你应该这样称呼它:

MyClass hl;
String base64String = Serialization.toString(hl);

如果您将String传递给序列化功能,则在致电String时会收到Serialization.fromString()。您希望获得一个可以转换为MyClass实例的对象,因此将其中一个传递到Serialization.toString()

答案 1 :(得分:0)

Serilization中的fromString()方法返回一个Object,你无法将其强制转换为MyClass类。以下行导致classCastException

MyClass hl = (MyClass) Serialization.fromString(items
    .getString("data"));