我的问题是关于反序列化使用一个类创建的文件"字符"和一个GUI表格" NewChar"。
我对Java相对较新,并尝试创建DnD样式字符生成器。我目前正在使用三个类 - Character(包含名称,性别,类,str,dex等实际统计信息的java类),其中包含构造函数;一个GUI表单(NewChar,使用NetBeans和Swing创建),允许用户选择Character类中定义的统计数据并根据需要自定义它们;和CharMain GUI页面应该(最终)向用户显示他们的角色并允许他们升级,选择武器等。
到目前为止,我可以通过在NewChar GUI框架中实例化类Character来成功地序列化数据(据我所知)。我还是无法从CharMain GUI页面加载序列化文件。首先,当我尝试readObject()时,问题似乎是ClassNotFoundException;在我尝试序列化的任何字符串上。其次,我继续收到EOF异常,虽然我怀疑这是因为序列化然后反序列化的项目数量自readObject()以来没有匹配;不工作。我用Google搜索并搜索了StackOverFlow并找到了一些针对类似问题的回复(例如:readobject method throws ClassNotFoundException或Java Mysterious EOF exception with readObject),但无法针对我的回复做出正面或反面的回答情况。
我曾尝试将我的Character类复制并粘贴到两个GUI表单中,但这似乎没有帮助。我还尝试仅序列化类Character的实例(实例名为createdChar),但这似乎也没有帮助。
我知道我试图反序列化的对象需要匹配已经序列化的对象(即包,类名等),但据我所知。我可能是超级盲人和/或愚蠢的忘记,但我的内部同事编码器似乎也无法弄清楚。在这一点上,我正在考虑将信息写入文本文件并将其读回而不是使用ObjectOutputStream和ObjectInputStream,但仍然真的想知道为什么这不起作用。请帮忙!
以下是序列化(然后反序列化)数据的代码:
private void viewCharActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String fileName = String.valueOf(createdChar.charName);
try {
FileOutputStream charSave = new FileOutputStream("/C:\\javaSaves\\newChar.sav");
ObjectOutputStream out = new ObjectOutputStream(charSave);
out.writeObject(createdChar.str);
out.writeObject(createdChar.dex);
out.writeObject(createdChar.con);
out.writeObject(createdChar.intel);
out.writeObject(createdChar.wis);
out.writeObject(createdChar.charisma);
out.writeObject(createdChar.strMod);
out.writeObject(createdChar.dexMod);
out.writeObject(createdChar.conMod);
out.writeObject(createdChar.intelMod);
out.writeObject(createdChar.wisMod);
out.writeObject(createdChar.charisMod);
out.writeObject(createdChar.baseAttack);
out.writeObject(createdChar.reflexSave);
out.writeObject(createdChar.willSave);
out.writeObject(createdChar.fortSave);
out.writeObject(createdChar.hp);
out.writeObject(createdChar.lvl);
out.writeObject(createdChar.speed);
out.writeObject(createdChar.armorClass);
out.writeObject(createdChar.size);
out.writeObject(createdChar.charName);
out.writeObject(createdChar.charRace);
out.writeObject(createdChar.charClass);
out.writeObject(createdChar.gender);
out.close();
charSave.close();
System.out.printf("Serialized data is saved on C:\\javaSaves\\newChar.sav");
}
catch (IOException i) {
i.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new charMain().setVisible(true);
}
});
closeFrame();
}
public void loadCharacterFromFile(){
try {
FileInputStream saveFile = new FileInputStream("/C:\\javaSaves\\newChar.sav");
ObjectInputStream save = new ObjectInputStream(saveFile);
createdChar.str = save.readInt();
createdChar.dex = save.readInt();
createdChar.con = save.readInt();
createdChar.intel = save.readInt();
createdChar.wis = save.readInt();
createdChar.charisma = save.readInt();
createdChar.strMod = save.readInt();
createdChar.dexMod = save.readInt();
createdChar.conMod = save.readInt();
createdChar.intelMod = save.readInt();
createdChar.wisMod = save.readInt();
createdChar.charisMod = save.readInt();
createdChar.baseAttack = save.readInt();
createdChar.reflexSave = save.readInt();
createdChar.willSave = save.readInt();
createdChar.fortSave = save.readInt();
createdChar.hp = save.readInt();
createdChar.lvl = save.readInt();
createdChar.speed = save.readInt();
createdChar.armorClass = save.readInt();
createdChar.size = (String) save.readObject();
createdChar.charName = (String) save.readObject();
createdChar.charRace = (String) save.readObject();
createdChar.charClass = (String) save.readObject();
createdChar.gender = (String) save.readObject();
save.close();
saveFile.close();
}
catch (IOException i) {
i.printStackTrace();
}
}