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

时间:2016-03-01 17:58:56

标签: java object bukkit objectinputstream objectoutputstream

所以我正在为Minecraft Spigot编写一个插件,但是我得到一个Java错误,这就是为什么我没有在Minecraft插件论坛上发布它,但无论如何我试图保存一个类将GroupManager称为File(带有一个名为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$1.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类。 以下是保存(/写入)的方法

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类的所有方法) 如果您在这里需要GroupManager类,那么它是:

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);
}

1 个答案:

答案 0 :(得分:2)

实施Serializable界面

public class GroupManager implements Serializable {