gpt4 book ai didi

java - 对象序列化不起作用

转载 作者:行者123 更新时间:2023-12-01 23:53:59 25 4
gpt4 key购买 nike

它会执行除了保存和/或加载数据之外所需执行的所有操作。我假设问题在于加载,即使 try/catch 没有异常。这些文件是由插件创建的,但它不会加载关闭时应该保存的数据...

完整的类可以在这里找到:http://pastebin.com/pK3NCPfM

加载方法:

    public void getClans(){
try {
// Load the Clans
FileInputStream f_in = new FileInputStream("Clans.data");
ObjectInputStream obj_in = new ObjectInputStream (f_in);
@SuppressWarnings("unchecked")
HashMap<String, Clan> Clans = (HashMap<String, Clan>) obj_in.readObject();
obj_in.close();
getLogger().info("Clans successfully loaded");
} catch (Exception e) {
getLogger().info("Error loading clan files...");
getLogger().info(e.getMessage());
}
}

public void getPlayers(){
try {
// Load the players that are in a clan
FileInputStream f_in = new FileInputStream("clanPlayers.data");
ObjectInputStream obj_in = new ObjectInputStream (f_in);
@SuppressWarnings("unchecked")
HashMap<String, String> clanPlayers = (HashMap<String, String>) obj_in.readObject();
obj_in.close();
getLogger().info("clanPlayers successfully loaded");
} catch (Exception e) {
getLogger().info("Error loading clanPlayer files...");
getLogger().info(e.getMessage());
}
}

更新:我不知道为什么这行不通。如果有人能帮我修复有问题的线路,我就可以走了!

    public void getPlayers(){
try {
// Load the players that are in a clan
FileInputStream f_in = new FileInputStream("clanPlayers.data");
ObjectInputStream obj_in = new ObjectInputStream (f_in);
@SuppressWarnings("unchecked")
clanPlayers = (HashMap<String, String>) obj_in.readObject(); // Error on this line.
//I want the HashTable ClanPlayers to be a copy of the object that was saved
obj_in.close();
getLogger().info("clanPlayers successfully loaded");
} catch (Exception e) {
getLogger().info("Error loading clanPlayer files...");
getLogger().info(e.getMessage());
}
}

最佳答案

这些方法不返回任何内容,也不初始化任何字段:仅初始化局部变量。所以它们基本上都是复杂的 noop。

此外,您不应该引发异常。捕获IOException,只有当你确实能够处理这个地方的异常时。否则,让异常传播,或者将其转换为 runeim 异常并抛出运行时异常。

关于java - 对象序列化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15883459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com