自定义对象序列化的Arraylist

时间:2014-03-12 14:47:29

标签: java serialization arraylist minecraft bukkit

我的代码中有两个数组列表。一个存储User对象,另一个存储Team对象。我要做的是当给出close指令时将两个数组列表保存到文件中,然后关闭程序。

我在保存两个AL时遇到了问题,但后来我定义了一个serialVersionUID,只有用户AL我们工作正常。我仍然得到AL队的错误。

保存错误:

14:29:46 [SEVERE] java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: org.bukkit.Location
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject(Unknown Source)
14:29:46 [SEVERE]       at java.util.ArrayList.readObject(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectInputStream.readObject(Unknown Source)
14:29:46 [SEVERE]       at plugin.Configs.loadTeams(Configs.java:129)
14:29:46 [SEVERE]       at plugin.UltimateSurvival.onEnable(UltimateSurvival.java:82)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.java:284)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.java:266)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:615)
14:29:46 [SEVERE]       at org.bukkit.Bukkit.reload(Bukkit.java:277)
14:29:46 [SEVERE]       at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
14:29:46 [SEVERE]       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:968)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:886)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:837)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
14:29:46 [SEVERE] Caused by: java.io.NotSerializableException: org.bukkit.Location
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at java.util.ArrayList.writeObject(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at plugin.Configs.saveTeams(Configs.java:52)
14:29:46 [SEVERE]       at plugin.UltimateSurvival.onDisable(UltimateSurvival.java:91)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:400)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:393)
14:29:46 [SEVERE]       at     org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:434)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:585)
14:29:46 [SEVERE]       ... 17 more

加载错误:

14:29:46 [SEVERE] java.io.NotSerializableException: org.bukkit.Location
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at java.util.ArrayList.writeObject(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:29:46 [SEVERE]       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.lang.reflect.Method.invoke(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject0(Unknown Source)
14:29:46 [SEVERE]       at java.io.ObjectOutputStream.writeObject(Unknown Source)
14:29:46 [SEVERE]       at plugin.Configs.saveTeams(Configs.java:52)
14:29:46 [SEVERE]       at plugin.UltimateSurvival.onDisable(UltimateSurvival.java:91)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
14:29:46 [SEVERE]       at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:400)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:393)
14:29:46 [SEVERE]       at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:434)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:585)
14:29:46 [SEVERE]       at org.bukkit.Bukkit.reload(Bukkit.java:277)
14:29:46 [SEVERE]       at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
14:29:46 [SEVERE]       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
14:29:46 [SEVERE]       at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:96
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:886)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:837)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
14:29:46 [SEVERE]       at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)

这是我目前正在开发的Minecraft插件的一部分。 一些代码:

public class Team implements Serializable
{
private static final long serialVersionUID = 6482400621090209853L;
private  String name = "";
private  int points = 0;
private  int size = 0;
private Location spawn = null;
}

public void loadTeams()
{
    try{
        FileInputStream file = new FileInputStream ("plugins/UltimateSurvival/teams.bin");
        ObjectInputStream ois = new ObjectInputStream(file);
        UltimateSurvival.teams = (ArrayList<Team>)ois.readObject();
        file.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

public void saveTeams()
{
    try
    {
        FileOutputStream file = new FileOutputStream ("plugins/UltimateSurvival/teams.bin");
        ObjectOutputStream oos = new ObjectOutputStream(file);
        oos.writeObject(UltimateSurvival.teams);
        file.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

我已经阅读了一些关于序列化的内容,但这是我第一次接触它。我做错了吗?

提前致谢

2 个答案:

答案 0 :(得分:1)

我刚刚

package tv.usasvideos.survivalgameteams;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SaveLoadList {
    public static void save(Object obj,String path) throws Exception {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) {
            oos.writeObject(obj);
            oos.flush();
        }
    }

    public static Object load(String path) throws Exception {
        Object result;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) {
            result = ois.readObject();
        }
        return result;
    }
}

private List<ChestPoint> chestpoints = new ArrayList<>();

@Override
public void onEnable(){
    try {
        chestpoints = (List<ChestPoint>)SaveLoadList.load("chestpoints.bin");
    } catch (Exception ex) {
        Logger.getLogger(SurvivalGame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void onDisable(){
    try {
        SaveLoadList.save(chestpoints,"chestpoints.bin");
    } catch (Exception ex) {
        Logger.getLogger(SurvivalGame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

答案 1 :(得分:0)

您的问题是Location无法序列化:

java.io.NotSerializableException: org.bukkit.Location

要使其可序列化,请先将其转换为字符串:worldNamexyz,您可以使用此字符串执行此操作:

public String locationToString(Location l){
    String world = l.getWorld().getName(); //get the world name
    Double x = l.getX(); //get the x coordinate
    Double y = l.getY(); //get the y coordinate
    Double z = l.getZ(); //get the z coordinate

    return world + "," + x + "," + y + "," + z; //return the location, but in string form
}

然后,如果你想从字符串中获取位置,你可以这样做:

public Location stringToLocation(String s){
    String[] str = s.split(","); //split s by ','
    World world = Bukkit.getWorld(str[0]); //get the world
    Double x = Double.parseDouble(str[1]); //get the x coord
    Double y = Double.parseDouble(str[2]); //get the y coord
    Double z = Double.parseDouble(str[3]); //get the z coord

    return new Location(world, x, y, z); //return a location created from the String
}

通过这样做,您现在已经使用LocationString转换为locationToString(),您可以将其放入数组中,然后,您可以使用String退出数组,并使用Location将其转换为stringToLocation()