写入文件时出现 ObjectInput 和 OutputStream 错误

ObjectInput and OutputStream error when writing in to a file

所以我正在为 Minecraft Spigot 编写一个插件,但是我收到一个 Java 错误,所以这就是为什么我没有在 Minecraft 插件论坛上发布它,但无论如何我正在尝试保存一个 class 将 GroupManager 调用到文件中(使用 class 称为 ObjectData) 之后我想再读一遍,这是我的错误信息:

[18:43:38 WARN]:        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
[18:43:38 WARN]:        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
[18:43:38 WARN]:        at playerapi.cubenex.ObjectData.write(ObjectData.java:27)
[18:43:38 WARN]:        at playerapi.cubenex.GroupManager.save(GroupManager.java:70)
[18:43:38 WARN]:        at playerapi.cubenex.ThePlugin.onDisable(ThePlugin.java:70)
[18:43:38 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:323)
[18:43:38 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:364)
[18:43:38 WARN]:        at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:424)
[18:43:38 WARN]:        at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:417)
[18:43:38 WARN]:        at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:458)
[18:43:38 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:708)
[18:43:38 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:535)
[18:43:38 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25)
[18:43:38 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
[18:43:38 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils.run(SourceFile:13)
[18:43:38 WARN]:        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[18:43:38 WARN]:        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
[18:43:38 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)

保存时的方法如下

public void save(){
    data.write(this);
}

当我阅读

public static GroupManager getGroupManager(Plugin plugin){
    DataFile file = new DataFile("groups.dot", plugin);
    if (!file.exists()) return new GroupManager(plugin);
    ObjectData data = new ObjectData(file);
    return (GroupManager) data.read();
}

GroupManager class。 这是保存(/写入)的方法

public void write(Object obj){
    file.delete();
    try {
        file.create();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        fos.close();
        oos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

当我阅读

public Object read(){
    try {
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        fis.close();
        ois.close();
        return obj;
    } catch (IOException | ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

(这些都是 ObjectData 中的所有方法 class) 如果您需要 GroupManager class,它是:

public class GroupManager {

ObjectData data;

private ArrayList<Group> groups;
private HashMap<Player, Group> players;

public GroupManager(Plugin plugin){
    data = new ObjectData(new DataFile("groups.dot", plugin));
    groups = new ArrayList<Group>();
}

public void add(Player p, Group g){
    players.put(p, g);
    if (p.isOnline()) g.addPlayer(p);
}

public void Joined(Player p){
    players.get(p).addBackPlayer(p);
}

public Group getGroup(Player p){
    return players.get(p);
}

public void registerGroup(Group group){
    groups.add(group);
}

public void deleteGroup(Group group){
    groups.remove(group);
}

public boolean containsGroup(Group group){
    return groups.contains(group);
}

public boolean containsGroup(String name){
    boolean r = false;
    for (int i = 0; i < groups.size(); i++){
        if (groups.get(i).getGroupName().equalsIgnoreCase(name)) r = true;
    }
    return r;
}

public Group getGroup(String name){
    Group r = null;
    for (int i = 0; i < groups.size(); i++){
        if (groups.get(i).getGroupName().equalsIgnoreCase(name)) r = groups.get(i);
    }
    return r;
}

public static GroupManager getGroupManager(Plugin plugin){
    DataFile file = new DataFile("groups.dot", plugin);
    if (!file.exists()) return new GroupManager(plugin);
    ObjectData data = new ObjectData(file);
    return (GroupManager) data.read();
}

public void save(){
    data.write(this);
}

实现Serializable接口

public class GroupManager implements Serializable {